Cannot zoom x axis properly with multiple 2D plots in same figure - linkprop?

I cannot figure out how to get the x axis to zoom properly. Y axis works fine because all of the y data is the same scale. But because I am plotting different xlim per plot linkaxes just can seem to do it. I have tried working with linkprop to no avail either, although maybe the answer is buried in there somewhere. I tried to simplify my code for posting but thought you might as well get it all and the data too, so links are provided. Any insight is appreciated.
data.png = Link to my plot
data.mat = Link to my data
figure
axesPosition = [.032 .01 .955 .99];
axes('position',axesPosition,'xtick',[],'ytick',[],'xTickLabel',{},'yTickLabel',{});
h = [];
for i = 1:size(disColWant,2)-1 + size(ctdColWant,2)-1;
if i <= size(disColWant,2)-1;
h(i) = axes('position',axesPosition);
plot(h(i), disNumData(:,disColNum(:,i+1)),...
(disNumData(:,(disColNum(:,1))))*-1,...
'linestyle',Config.LineStyle.(disColWant{i+1}),...
'color',Config.Color.(disColWant{i+1}),...
'marker',Config.MarkerStyle.(disColWant{i+1}),...
'markersize',Config.MarkerSize);
set(gca,'xlim',Config.Scale.(disColWant{i+1}),...
'ylim',Config.Scale.(disColWant{1}),...
'visible','off');
else h(i) = axes('position',axesPosition);
plot(h(i),ctdNumData(:,ctdColNum(:,i-11)),...
(ctdNumData(:,(ctdColNum(:,1))))*-1,...
'linestyle',Config.LineStyle.(ctdColWant{i-11}),...
'color',Config.Color.(ctdColWant{i-11}),...
'marker',Config.MarkerStyle.(ctdColWant{i-11}),...
'markersize',Config.MarkerSize);
set(gca,'xlim',Config.Scale.(ctdColWant{i- 11}),...
'ylim',Config.Scale.(ctdColWant{1}),...
'visible','off');
end
end
linkaxes(h,'y')

 Accepted Answer

Create a zoom mode object for the figure. Use a ButtonDownFilter to filter out requests that are not over any of the axes. Create a ActionPostCallback that figures out which axes was zoomed and then goes around to all of the other axes and does appropriate setting of the axes limits for them.
Or instead of using ButtonDownFilter and having to figure out which axes it was, you could (depending on what else you need to do) set HitTest off on all of the objects in all of the axes, leaving only a single axes (but not its children) with HitTest on. The zoom would then be known to be against that one axes, so you could skip the ButtonDownFilter and checking of which axes was touched and go ahead and set the xlim appropriately for all of the other axes.

4 Comments

Thanks for the quick reply, but you are way over my head. Maybe there is a different way to plot with the same aesthetic result but with x and y zoom capability?
Sure there is, if you want to play with event listeners.
zooming is inherently handled by setting the xlim and ylim. Your ylim are the same for all of your plots, but you have multiple axes each with its own xlim. linkprop() cannot do the trick because it ensures that the linked properties are the _same_ on all of the linked objects, whereas you need different xlim all kept in the same relative proportions.
I do note that you set the axes visibility off for each of the axes. This suggests a hack: use the same xlim for all of the axes (and use linkaxes()) but scale the x locations that you use to plot with so that what gets submitted as the x maps the actual data range in to the same data range. Doing that would be messy for axis ticks and labels, but you do not use those. Doing this would also interfere with the use of datacursors when used in default mode, but it is possible to change the datacursor callbacks to unmap the coordinates and display the original coordinates of the underlying object... if you even care about data cursors.
Thanks for sticking with this. You are correct, the x axes do not matter when plotting, but it would be convenient if the datacursor function was an option. As it is now however, x values do not matter as their relative positions are being analysed subjectively. Scaling all the xlims could definitely work, but how I would actually go about that will take me some time to wrap my head around. You wouldn't have a suggestion would you?
I have decided to skip the x axes dilemma altogether. Thanks a lot for your insight.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!