3

I have what I think is a simple problem: I have a matrix that I image using imagesc. I simply want to show a second y-axis on the right hand side of the image. How do I do that? Example:

clear all;
aMatrix = rand(20,30);
yAxis1 = 32.*(1:size(aMatrix,1));
yAxis2 = 165.*(1:size(aMatrix,1));
xAxis = 1:size(aMatrix,2);
imagesc(yAxis1, xAxis1, aMatrix);

The following will show the image with yAxis1, on the left hand side. That is great, but how do I show yAxis2 on the right hand side of the image at the same time? Thanks.

lennon310
  • 12,503
  • 11
  • 43
  • 61
Spacey
  • 2,941
  • 10
  • 47
  • 63

2 Answers2

6
  aMatrix = rand(20,30);
  yAxis1 = 32.*(1:size(aMatrix,1));
  yAxis2 = 165.*(1:size(aMatrix,1));
  xAxis = 1:size(aMatrix,2);
  h1=imagesc(xAxis, yAxis1, aMatrix);set(gca,'YDir','normal');
  ax1=gca;
  set(ax1,'YColor','r','YAxisLocation','right');
  set(ax1,'XTickLabel',' ');
  ax2=axes('Position',get(ax1,'Position'),'YAxisLocation','left');
  h2=imagesc(xAxis, yAxis2,aMatrix,'Parent',ax2);
  set(gca,'YDir','normal');

enter image description here

lennon310
  • 12,503
  • 11
  • 43
  • 61
  • Thanks, but can you please elucidate what is going on exactly? – Spacey Jan 09 '14 at 14:46
  • @Learnaholic it covers two images together in the same figure (in your case it is the same aMatrix image). ax1 is the figure handle that the y axis is set on the right, when you imagesc the image for 2nd time, you first get the axes info before imagesc. The position of the 1st image is obtained via get(ax1,'Position'). And the y axis is set on the left. – lennon310 Jan 09 '14 at 14:51
  • I do not understand at all what is going on. When you first make an image, its default yaxis is to the left. Why do you have 'xticklabel'? We are not dealing with xaxis. Then in ax2 you set it back to the left?? It is supposed to be to the right...then in the end you set Ydir to 'normal' again... why? – Spacey Jan 09 '14 at 14:56
  • set(ax1,'YColor','r','YAxisLocation','right'); is set the yaxis on the right. You can remove this line and change YaxisLocation in ax2 to 'right'. Without the 'normal' setting in Ydir, the labeling sequence will be reversed (smaller number on the top). Check this page for more options on axes: http://www.mathworks.com/help/matlab/ref/axes_props.html You can try to remove each line and observe the changes in display. Thanks – lennon310 Jan 09 '14 at 15:01
0

A quite similar solution using yyaxis :

  aMatrix = rand(20,30);
  yAxis1 = 32.*(1:size(aMatrix,1));
  yAxis2 = 165.*(1:size(aMatrix,1));
  xAxis = 1:size(aMatrix,2);
  yyaxis left
  imagesc(xAxis, yAxis1, aMatrix);
  ax = gca;
  ax.YColor = ax.XColor;
  ylabel('Left Side')
  yyaxis right
  imagesc(xAxis, yAxis2,aMatrix);
  ax.YColor = ax.XColor;
  ylabel('Right Side')  
kaj
  • 1