Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two y axis with the same x-axis [duplicate]

Tags:

add

matlab

axis

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?!

like image 858
tim Avatar asked Jul 04 '11 16:07

tim


People also ask

What is a double Y axis graph?

A second Y axis is a Y axis drawn on the right-hand side of a chart. It can show the same axis scale as the primary Y axis or a different scale. You can use a second Y axis with the same scale as the primary Y axis on a wide chart to help viewers interpret the data more easily.

Can you have 2 y axis?

When the data values in a chart vary widely from data series to data series, or when you have mixed types of data (for example, currency and percentages), you can plot one or more data series on a secondary vertical (Y) axis.


1 Answers

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.

like image 124
mbauman Avatar answered Oct 19 '22 10:10

mbauman