File Exchange

image thumbnail


version (3.54 KB) by Kelsey Bower
Adds zoomed plot inset to current axes.


Updated 23 Oct 2016

View License

Editor's Note: This file was selected as MATLAB Central Pick of the Week

Adds a zoomed plot inset to current axes, for use in highlighting a subarea of the current plot.
[p,z] = zoomPlot(x,y,xbounds,pos,vertex) where:
x,y = vectors being plotted
xbounds = [x1 x2] specifies the zoom indices, where x1 is the
first x value displayed in the zoom plot and x2 is the last.
pos = [left, bottom, width, height] specifies the location and
size of the side of the zoom box, relative to the lower-left
corner of the Figure window, in normalized units where (0,0)
is the lower-left corner and (1.0,1.0) is the upper-right.
vertex = toggles connecting lines corresponding to vertices, where 1
corresponds to the top left vertex and continuing clockwise,
4 corresponds to the bottom left vertex. Any/all 4 vertices can
be included.

p = axes handle for larger plot
z = axes handle for zoom plot
Note that the title, labels, and legend of the larger plot should be designated BEFORE placing the zoom plot. Otherwise, zoomPlot returns the handle of the original axes (p), and the title can be changed using "p.Title.String = title_string", etc.

% x and y data
x = [0; 5; 10; 10.1; 10.25; 10.5; 11.34; 22.83; 43; 119; 257];
y = 2*exp(-1/8*x)+1/3*exp(-2*x)+0.2*rand(length(x),1);
hold on

% fit an exponential function to data
f1 = fit(x,y,'exp2');

% set major axes legend, title, and labels
title('How far does the apple fall?')
xlabel('Distance from tree')
legend('Experimental Data','Multi-Exponential Fit')

% zoomPlot to highlight a portion of the major plot
[p,z] = zoomPlot(x,y,[5 50],[0.33 0.35 0.3 0.55],[1 3]);
hold on
legend hide

% Forgot to add a title/label/legend to the original plot?
p.YLabel.String = 'Apples';

Kelsey Bower ( 10/20/15

Cite As

Kelsey Bower (2021). zoomPlot (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (12)

Phuc Bui

The most simple way to avoid "data1", "data2" is turning of the autoupdate property of the legend: legend({'A','B'},'AutoUpdate','off')

Nik Rocky

That can I do when I want to zoom a diagram with 6 different data lines, Dots and spectrum in background. Is it possible with your tool?


Xingwang Yong


@chen yahui
I found an answer to delete the legend 'data1' and 'data2', at here:

just add these lines at the bottom of the example
h1 = p.Children(1);
h2 = p.Children(2);

Raúl Ibáñez Couoh


chen yahui

It works well. But I only have one legend for my major plot, right now it adds another symbol 'data1' which is indicate axis of the zoom plot, I want to know how to delete this legend, use legend hide no function

Michael Ilewicz

the vertexes appear misplaced. I would appreciate a fix

Stuart Gilchrist



Albert Feeny


MATLAB Release Compatibility
Created with R2015a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!