Hi all,
I am trying to get a second y axis into an subplot.
figure
handle1 = subplot(3,3,1)
handle2 = axes('YAxisLocation','right','Color','none', ...
'XGrid','off','YGrid','off','Box','off');
This creates an additional axis object on the edge of the whole figure and not on the edge of the subplot. Setting the parent property of axes to handle1 is not possible. Calling axes(handle1) does not help. plotyy() works but has a strange behavior in a certain way. Therefore I want to use a own axis. I have not figured out what plotyy does to solve this problem.
Do you have an idea?
Thanx, Milan

 Risposta accettata

Matt Fig
Matt Fig il 16 Nov 2012
Modificato: Matt Fig il 16 Nov 2012
You can use SUBPLOT to set the location for your PLOTYY call.
% Data to plot.
x = 0:0.01:20;
y1 = 200*exp(-0.05*x).*sin(x);
y2 = 0.8*exp(-0.5*x).*sin(10*x);
% Let's set up our subplot by relying on SUBPLOT
figure
AX = subplot(2,1,1); % Auto-fitted to the figure.
P = get(AX,'pos'); % Get the position.
delete(AX) % Delete the subplot axes
[AX,H1,H2] = plotyy(x,y1,x,y2,'plot');
set(AX,'pos',P) % Recover the position.

4 Commenti

Matt Fig
Matt Fig il 16 Nov 2012
Modificato: Matt Fig il 16 Nov 2012
Milan comments:
Hi Matt,
thank you for your fast reaction. Nice solution. The problem I had with plotyy() is, that the limit of the axes is fixed and the scaling is not adapted automatically to new data.
% Data to plot.
x = 0:0.01:20;
y1 = 200*exp(-0.05*x).*sin(x);
y2 = 0.8*exp(-0.5*x).*sin(10*x);
% Let's set up our subplot by relying on SUBPLOT
figure
AX = subplot(2,1,1); % Auto-fitted to the figure.
P = get(AX,'pos'); % Get the position.
delete(AX) % Delete the subplot axes
[AX,H1,H2] = plotyy(x,y1,x,y2,'plot');
set(AX,'pos',P) % Recover the position.
line([0 10],[0 500],'parent',AX(1)) % Axis is not scaled
axis auto % Numbers on axes are crushing.
I do not know how to solve this plotyy() problem. I solved the problem not using plotyy().
figure
AX(1) = subplot(3,3,1);
ax1hv = get(AX(1),'HandleVisibility');
AX(2) = axes('HandleVisibility',ax1hv,'Units',get(AX(1),'Units'), ...
'Position',get(AX(1),'Position'),'Parent',get(AX(1),'Parent'));
set(AX(2),'YAxisLocation','right','Color','none', ...
'XGrid','off','YGrid','off','Box','off');
linkaxes(AX,'x');
line([0 1],[0 500],'parent',AX(1))
axis auto
I looked at plotyy() again, and I found it there.
Regards,
Milan
Milan, your problem is easily addressed! Try this:
% Data to plot.
x = 0:0.01:20;
y1 = 200*exp(-0.05*x).*sin(x);
y2 = 0.8*exp(-0.5*x).*sin(10*x);
% Let's set up our subplot by relying on SUBPLOT
figure
AX = subplot(2,1,1); % Auto-fitted to the figure.
P = get(AX,'pos'); % Get the position.
delete(AX) % Delete the subplot axes
[AX,H1,H2] = plotyy(x,y1,x,y2,'plot');
set(AX,'pos',P) % Recover the position.
line([0 10],[0 500],'parent',AX(1)) % Axis is not scaled
axis auto % Numbers on axes are crushing.
set(AX(1),'ytickmode','auto') % This is new....
Milan Matah
Milan Matah il 16 Nov 2012
There is alway a simple solution in matlab... Now I know the one for this issue. Thanx
rfigueiredo
rfigueiredo il 5 Giu 2015
Hi! I was having the same problem but with this previous answer I couldn't resolve it because a blue line appears in the middle of the plot and I don't how to remove it. Another error that comes across is that the legend for the two plotted graphics is blue for both, when it should be red and blue. I think the main problem is in the appearance of the straight line in the middle of the graphic(this line seems to begin in the origin of the plot and end in the coordinate that represents the final point of both the x and y limit). Does anyone knows how to resolve this problem? Thank you in advance! Raquel

Accedi per commentare.

Più risposte (2)

Valentino
Valentino il 14 Lug 2016
yyaxis(ax(1),'left')
plot(ax(1),x1,y1,'Color', Color_left)
set(ax(1),'ylim', [0,300])
set(ax(1),'YColor',Color_left);
ylabel(ax(1),('Label left '}))
yyaxis(ax(1),'right')
plot(ax(1),x2,y2,'Color', Color_right)
set(ax(1),'ylim', [-10,15])
set(ax(1),'YColor',Color_right);
ylabel(ax(1),({'Label right'}))
As yyaxis is new introduced in 2016a i figured this might help somebody.

1 Commento

Nachiket Wadwankar
Nachiket Wadwankar il 19 Ott 2018
Thanks for this solution. This is exactly what I was looking for.

Accedi per commentare.

Ghulam Murtaza
Ghulam Murtaza il 7 Mag 2014

0 voti

Hi Matt,
I want to use two separate y axis in a subplot. However, another thing is that my plot types for both is different as well.
One is a scatter plot with first y axis and second is stairs plot with the second y axis. Could you kindly help me with that.
Thanks in advance, Ghulam

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by