Tiledlayout and duplicating plot for zoom of original plot

49 views (last 30 days)
Hi all,
I have a plot with different xlines and text annotations with somewhat a large dataset (~3M datapoints).
I am trying to create a subplot (actually a tiledlayout) view where I have the original full graph displayed on the top plot and a zoomed view of the same plot below. I am looking to create a copy of the first plot, where I'll change the Y axis to zoom the graph. This might not be the most optimal way of doing this, however it is a start.
I have created the following testcode to figure out how to do this:
A= rand(50,1)*10;
B= rand(5,1)*length(A);
figT=tiledlayout(2,1);
FigTile1= nexttile(1);
figA = plot(A); hold on;
for i=1:length(B)
xline(B(i),'r')
end
hFigIAxes = findobj('Parent',figT,'Type','axes');
FigTile2 = nexttile(2);
copyobj(FigTile2,hFigIAxes)
But this just throws an error saying "Axes cannot be a child of Axes.". I am not quite sure how to correct this.
Any help will be welcomed! Thanks, Tommy

Accepted Answer

Adam Danz
Adam Danz on 9 Aug 2023
Edited: Adam Danz on 9 Aug 2023
Some feedback on your test code.
hFigIAxes = findobj('Parent',figT,'Type','axes');
No need to do this. You already have the axes handle, FigTile1 which is the same as hFigIAxes.
copyobj(FigTile2,hFigIAxes)
Here you are copying the first axes to the second axes but axes cannot be a child of another axes. I think what you meant to do is to copy the children of the first axes to the second axes:
copyobj(FigTile1.Children, FigTile2)
The result would look like this. Then, you can add the additional steps of adjusting the axes limits.
A= rand(50,1)*10;
B= rand(5,1)*length(A);
figT=tiledlayout(2,1);
FigTile1= nexttile(1);
figA = plot(A); hold on;
for i=1:length(B)
xline(B(i),'r')
end
FigTile2 = nexttile(2);
copyobj(FigTile1.Children, FigTile2)
box(FigTile2, 'on')
I can point you to other solutions to adding a magnifier axes in case you are interested. There are many more, these are the ones I'm familiar with.
  2 Comments
Tommy Bechsgaard
Tommy Bechsgaard on 9 Aug 2023
Hi Adam,
This is exactly what I was trying to accomplish - much appreciated!
FYI: My dataset consists of high amplitude peaks on top of some low amplitude signals, both are important to look at, which is why i need the full view and the zoomed part of the Y-axis to look at the small amplitude signals.
Have a good one!
Best,
Tommy
Adam Danz
Adam Danz on 9 Aug 2023
You may want to use linkaxes(ax,dimension) to ensure the x-axis limits always match between the two axes.
linkaxes([FigTile1,FigTile2], 'x')

Sign in to comment.

More Answers (0)

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!