different limits for 2 yaxis

I'm generating a plot with 2 yaxis showing two datasets with larger differences in their values, e.g.
data1 = 100 + (200-100).*rand(365,1);
data2 = 1 + (2-1).*rand(365,1);
time = 1:365;
figure(1);
ax1 = axes('position',[0.05 0.5 0.22 0.37]);
plot(time,data1,'k','linewidth',1);
ylabel('label 1');
hold on;
plot(time,data2,'color',[.5,.5,.5],'linewidth',1);
add = gca;
extra = axes('Position',get(add,'Position'),'Color','none','XTick',[],'YAxisLocation','right');
ylabel(extra,'label 2');
linkaxes([add extra],'xy');
From this, how would I have a different ylim for each data set? In other words apply a different ylim to both yaxis. Please note that I do not wish to use plotyy and was hoping of some advice following the method I have shown above.

 Akzeptierte Antwort

Azzi Abdelmalek
Azzi Abdelmalek am 13 Sep. 2012

2 Stimmen

close all;
data1 = 100 + (200-100).*rand(365,1);
data2 = 1 + (2-1).*rand(365,1);
time = 1:365;
figure(1);
ax1 = axes('position',[0.05 0.5 0.22 0.37]);
plot(time,data1,'k','linewidth',1);
ylabel('label 1');
pos=double(get(ax1,'position'));
ax2=axes('position',pos,'color','none','YAxisLocation','right','xtick',[])
hold on;plot(time,data2,'r','linewidth',1,'parent',ax2);
ylabel(ax2,'label 2');

Weitere Antworten (1)

Jan
Jan am 13 Sep. 2012
Bearbeitet: Jan am 13 Sep. 2012

0 Stimmen

If you have the handles of both axes, you can simply use them to set the Y-limits:
set(ax1, 'YLim', [0, 10]);
set(extra, 'YLim', [-10, 5]);
Or:
ylim(ax1, [0, 10]) % etc

3 Kommentare

Richard
Richard am 13 Sep. 2012
This doesn't seem to work, your code returns an error: Error using set Bad property value found. Object Name : axes Property Name : 'YLim' Value must be a 2 element vector. By changing it to set(ax1, 'YLim', ([0 10])); set(extra, 'YLim', ([-10 5])); It changes both ylimits to -10:5
Matt Fig
Matt Fig am 13 Sep. 2012
Iestyn, I ran your code, then Jan's code and got no error. What version are you using?
Richard
Richard am 13 Sep. 2012
Its 2012, I only have a few toolboxes though if that makes any difference (statistics, wavelet, and signal processing). I receive no error whilst typing ylim(ax1, [0, 10]) and ylim(extra, [0, 20]) but it doesnt change the limits

Melden Sie sich an, um zu kommentieren.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by