How do I create a plot within another plot?
20 views (last 30 days)
Show older comments
MathWorks Support Team
on 25 Aug 2010
Commented: Walter Roberson
on 6 Sep 2022
I would like to generate a second set of axes within a plot to display a detailed view of a part of the original figure.
Accepted Answer
MathWorks Support Team
on 30 Jun 2017
The ability to use a detailed blown-up figure within a main figure window is not automatically available in MATLAB. To work around this issue, execute the script demonstrated in the following example.
1. Create data and plot the primary figure.
x = -pi:0.01:pi;
y = 2*sin(12*x) + 3*cos(0.2*pi+2);
plot(x,y, 'k');
2. Find the position of the current axes and then create a small axes using that position data.
p = get(gca, 'Position');
h = axes('Parent', gcf, 'Position', [p(1)+.06 p(2)+.06 p(3)-.5 p(4)-.5]);
3. You can now plot data on the second axes and zoom in on specific points.
plot(h, x, y, 'r');
set(h, 'Xlim', [-0.2 0], 'Ylim', [-5 -4]);
4. You can also turn off the tick marks on your small axes.
set(h, 'XTick', [], 'YTick', []);
After the axes are positioned as desired, You can add a rectangle and text arrow to visually link the data on the small axes with the data on your origional axes. From the figure window drop-down menu, you may select “insert->rectangle” and “insert->arrow”.
1 Comment
Walter Roberson
on 6 Sep 2022
You can use the above techniques to insert a second axes with the same position as the current axes. You might need extra work with linkaxes() to have the extra axes automatically reposition when the tile repositions as more tiles are added.
More Answers (0)
See Also
Categories
Find more on Data Exploration in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!