Two y axes with the same x axis

Possible duplicate:
Drawing 4 curves in one plot with 3 axes

Assuming I have the following data set as an example here in Matlab:

x = linspace(0, 9, 10); y1=arrayfun(@(x) x^2,x); y2=arrayfun(@(x) 2*x^2,x); y3=arrayfun(@(x) x^4,x); 

So you can see that they have ONE x axis. Now I want the following plot:

one x axis with limits from 0 to 9 (these restrictions must also be ticks) with N ticks (I want to be able to determine N myself), thereby having N-2 ticks inbetween, because 0 and 9 are ticks themselves. I want y1 and y2 to refer to the same y axis that is displayed on the left, with ticks for 0 and max ([y1, y2]) and M have more ticks between them. than I want to have a different axis on the right, where y3 refers to ...

y1, y2 and y3 must have entries in the same legend field ... thanks so far!

edit: argh just found this: Drawing 4 curves on the same plot with 3 axes , maybe I can do it myself ... I'll try it now!

EDIT: What about using the logarithmic x-axis ?!

+7
source share
1 answer

See this documentation for Using Multiple X-Axis and Y-Axis . Something like this should do the trick:

 figure ax1 = gca; hold on plot(x,y1) plot(x,y2) ax2 = axes('Position',get(ax1,'Position'),... 'XAxisLocation','top',... 'YAxisLocation','right',... 'Color','none',... 'XColor','k','YColor','k'); linkaxes([ax1 ax2],'x'); hold on plot(x,y3,'Parent',ax2); 

Edit: screams, missed a hold command. Should work now. Alternatively, to remove the second X axis from above, simply add 'XTickLabel',[] to the axes command.

As an aside, you really shouldn't use arrayfun for y1=arrayfun(@(x) x^2,x); . Instead, use the .^ Operator: y1=x.^2; . This is a much better style and much faster.

+12
source

All Articles