Flase color plot with two, different X-Axes, keeping it edieditable.

3 views (last 30 days)
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

Sebastian89
Sebastian89 on 10 Nov 2017
Hello, I managed to get it working.
function mypostcallback(obj,evd,AX,ax2)
AX=gca;
set(ax2,'Position',AX.Position,'Color','none','XLim',AX.XLim,'YLim',AX.YLim);
XLimc=get(AX,'XTickLabel'); %Getting Label of bottom axis
XLim(length(XLimc))=0;
for i=1:length(XLimc)
XLim(i)=str2num(XLimc{i,1});
end
X2Lim=XLim*10; % calculat Label for Top axis
set(ax2,'XTickLabel',X2Lim) % change label
axes(AX)
maybe there is a better way to do it.

More Answers (1)

Amy
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
Sebastian89 on 9 Nov 2017
Edited: Sebastian89 on 9 Nov 2017
Hello Amy, Thanks for your answer. I think i get the basic concept. i should creat a secound axis like the following and the link it with the funktion to update it. so in my case it would be best to give the function both axis.
%%-- Testscript -- %%
E= randn(5,5);
X=1:5;
Y=10:14;
imagesc(X,Y,E);
colormap('jet');
c = colorbar('eastoutside');
c.Label.String = 'delta OD';
AX=gca;
ax2 = axes('Position',AX.Position,'Color','none','XLim',AX.XLim,'YLim',AX.YLim);
set(ax2,'YColor','none');
set(ax2,'XAxisLocation','top')
XLimc=get(AX,'XTickLabel'); %Getting Label of bottom axis
XLim(length(XLimc))=0;
for i=1:length(XLimc)
XLim(i)=str2num(XLimc{i,1});
end
X2Lim=XLim*10; % calculat Label for Top axis
set(ax2,'XTickLabel',X2Lim) % change label
axes(AX)
h = zoom(AX);
set(h,'ActionPostCallback',{@mypostcallback,AX,ax2});
set(h,'Enable','on');
The datetick convert it into a date, which is no use for me.
function mypostcallback(obj,evd,AX,ax2)
AX = gca;
XLimc=get(AX,'XTickLabel'); %Getting Label of bottom axis
XLim(length(XLimc))=0;
for i=1:length(XLimc)
XLim(i)=str2num(XLimc{i,1});
end
X2Lim=XLim*10;
set(ax2, 'XTick',AX.XTick) %Set XLabel Number and position?
set(ax2,'XTickLabel',X2Lim) % change label
At the moment i just can not set a New Position for the Labels. Kind regards Sebastian

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!