7

Possible Duplicate:
Plotting 4 curves in a single plot, with 3 y-axes

assuming I have the following dataset 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);

thus you can see they have the SAME x-axis. Now I want the following plot:

one x-axis with the limits 0 to 9 (those limits should also be ticks) with N ticks (I want to be able to define N myself), thus having N-2 ticks inbetween because 0 and 9 itself are already ticks. I want y1 and y2 to refer to the same y-axis, which is being displayed on the left with ticks for 0 and max([y1, y2]) and M more ticks inbetween. than I want to have another axis on the right, where y3 refers to...

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

edit: argh just found this: Plotting 4 curves in a single plot, with 3 y-axes perhaps I can bould it up myself... I will try just right now!

EDIT: What when using logarithmic x-axis?!

3
  • 1
    For your purposes, plotyy should be sufficient
    – mbauman
    Jul 4, 2011 at 16:19
  • Thanks yeah, I got it to work by using this: mathworks.com/matlabcentral/fileexchange/9016-addaxis I would rather want to use plotyy but I don't see how to plot x vs y1 and y2 on the same axis and only x vs y3 on the other one!? Because plotyy only takes the arguments (x,y1,x,y2)
    – tim
    Jul 4, 2011 at 16:32
  • just FYI: Its not a duplicate.
    – tim
    Mar 7, 2015 at 17:32

1 Answer 1

12

See this documentation on Using Multiple X- and Y-Axes. 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: whoops, missed a hold command. Should work now. Also, to remove the second x-axis on top, 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;. It's much better style and is much quicker.

6
  • This doesn't work for me. Only when I remove the last plot command I see the second axis (but than I have a second x-axis on top). When using the third plot command, the y-axis is being displayed additionally at the left.
    – tim
    Jul 4, 2011 at 16:51
  • 1
    Teaches me to post code that I didn't test. I missed the hold on the second axes. I've now tested it and it works for me.
    – mbauman
    Jul 4, 2011 at 17:24
  • 1
    @Matt B.: I would replace all PLOTs with lower-level LINEs (that way you don't need hold commands), give them distinct colors, save their handles in a vector, and add a legend: legend(hLines, 'y1','y2','y3')
    – Amro
    Jul 4, 2011 at 17:27
  • @MattB: First let me say thanks! You are right about arrayfun, it was just an quick (in this case bad) example... I'm gonna try it out!
    – tim
    Jul 4, 2011 at 18:07
  • 1
    +1 This is a better answer with more detail given than the accepted answer to the question this is a duplicate of. Jul 11, 2012 at 17:39

Not the answer you're looking for? Browse other questions tagged or ask your own question.