Flase color plot with two, different X-Axes, keeping it edieditable.
Show older comments
Hello everyone,
i want to use imagesc() with 2 depending x axes. For example ax1 is the time and ax2 is the travelt distance (constante velocity). So There is just a function like distance=time*velocity witch change the number of the axes. At the moment i try something like this:
X=1:10;
Y=1:10;
Z=randn(10,10);
Velocity=33;
imagesc(X,Y,Z);
ax1=gca;
xlabel('Time');
ylabel('Wavelength');
ax2 = axes('Position',ax1.Position,'Color','none');
ax2.XAxis.TickLabelFormat=('%.2f'); %Does not work for Label
ax2.XAxisLocation = 'top';
ax2.XTick= ax1.XTick;
ax2.YLim=[min(Y),max(Y)];
ax2.YTick = [];
ax2.XTickLabel=(X*Velocity); %function for chancing x1 in x2
xlabel('Distance');
%ax2.XTickMode = 'auto';
linkaxes([ax1,ax2],'xy')
But if i want to change the colorbar the Plot get ugly. The Position of the box arn't correct anymore (i get a littel offset off ax1 and ax2). Another drawback ist, that the ax2 is fix, so if i want to look a a closer region the ax2 may disappear to missing ticks.
At the moment look like to layer, because i can have two different colorbars. One representing my Data and an emty one from 0 to 1 values.
To set the Colorbar automatical is not an option, i need to by flexible.
Hope there is an soulution. I may use a different function than imagesc, but i want to present it as a 2D false Image plot.
Thanks you in advance. Im still useing Matlab2016b
PS:edit minimal exampel for being fully functional
Accepted Answer
More Answers (1)
Amy
on 26 Oct 2017
Hi Sebastian,
'linkaxes' makes it so that your two axes have identical limits, but the issue here is that it is not choosing limits where you actually have data plotted, which is why you are seeing some empty space after the call to 'linkaxes'.
You aren't actually plotting data in the second axis, so you might as well set it to have the same limits as the first one when you create it, like this:
ax2 = axes('Position',ax1.Position,'Color','none','XLim',ax1.XLim,'YLim',ax1.YLim);
You set the 'XTick' and 'XTickLabels' properties for your second axis manually which is why they aren't automatically updated when you zoom. You can define your own zoom mode callback function that updates them, however.
There's an example of setting such a callback function in this MATLAB answers post, which you could modify to apply to your use case.
1 Comment
Sebastian89
on 9 Nov 2017
Edited: Sebastian89
on 9 Nov 2017
Categories
Find more on Axis Labels in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!