Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Left & Right yaxislocations with imagesc

Subject: Left & Right yaxislocations with imagesc

From: Mike Fowler

Date: 13 Mar, 2006 10:13:34

Message: 1 of 3

Hi folks,

I'm trying to plot using imagesc, and I would like to put (different)
y-axis labels on both the left and right y-axes, but haven't found
out how to do this yet.

The regular commands for plotting 2 y-axes (or plotyy) don't seem to
work here - the yaxislocation commands.

e.g.
============================
X = unifrnd(-1,1,11,11);
subplot(221);
h11=imagesc(X,[-1 1]);
ax1=gca;
ax2 = axes('Position',get(ax1,'Position'),'YAxisLocation','right');
h21 = imagesc(flipud(test6),[-1 1]);
set(h21,'Parent',ax2,'yaxislocation','right');
============================

doesn't work as I want. It returns an error, and the yaxis to the
left side. I've managed to get left and right y-axis in a full figure
plot (not a subplot), through the Property Editor GUI, but need to
edit multiple subplots, and can't find the right syntax.
If anyone can help out here I'd be very grateful!

Thanks,
Mike

Subject: Left & Right yaxislocations with imagesc

From: Adam A

Date: 2 May, 2012 09:19:07

Message: 2 of 3

A very old post, but I've been looking for an answer to the same problem. Here's some code that can be used as a workaround:

figure

% Create two axes:
h1 = axes;
h2 = axes;

% Place the imagesc data in axes h2:
imagesc(rand(5,10),'Parent',h2);

% Label the axes:
xlabel(h1,'x1')
xlabel(h2,'x2')
ylabel(h1,'y1')
ylabel(h2,'y2')
set(h1,'YAxisLocation','left')
set(h2,'YAxisLocation','right')
set(h1,'XAxisLocation','bottom')
set(h2,'XAxisLocation','top')

% Create the colorbar:
hc = colorbar('vert');
ylabel(hc,'color')

% Make the two axes have matching dimensions:
temp1 = get(h1);
temp2 = get(h2);
set(h1,'Position',temp2.Position,'XDir',temp2.XDir,'XLim',temp2.XLim,'XTick',temp2.XTick,'YDir',temp2.YDir,'YLim',temp2.YLim,'YTick',temp2.YTick)

Subject: Left & Right yaxislocations with imagesc

From: Adam A

Date: 2 May, 2012 11:06:09

Message: 3 of 3

Also, although the code above doesn't work with Matlab's subplot command, you can work around that by positioning the axes manually, by substituting in the lines below:

% Create two axes:
h1 = axes('Position',[0.1,0.6,0.8,0.3]);
h2 = axes('Position',[0.1,0.6,0.8,0.3]);

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us