필터 지우기
필터 지우기

How do I create a single line plot (just one line) with two differently scaled y-axes?

조회 수: 14 (최근 30일)
I'm looking to create just one single line on a plot with two differently scaled y-axes--the data I need to plot is linearly converted from one value to another, so the shape of the resulting lines is identical. plotyy produces two different lines (which I don't want). Is there an easy way to do this?

답변 (3개)

TastyPastry
TastyPastry 2015년 11월 4일
You can use plotyy() to plot two lines on top of each other so you only see one line. If the colors need to be the same, you can adjust them.
x = 1:100;
y = 1:100;
y1 = 101:200;
plotyy(x,y,x,y1);
  댓글 수: 1
Brigitta Rongstad
Brigitta Rongstad 2015년 11월 4일
I have tried this, however the scales are not aligned perfectly and my lines are still slightly offset. Is there a way to make the scales align?

댓글을 달려면 로그인하십시오.


Star Strider
Star Strider 2015년 11월 4일
편집: Star Strider 2015년 11월 4일
I would set 'Visible','off' for the second line. You can do that with its handle (that you have to request as an output).
Example:
TC = 5:35;
TF = 1.8*TC + 32;
x = 1:length(TC);
figure(1)
[hax,hline1,hline2] = plotyy(x,TC, x,TF);
set(hline2, 'Visible','off')

Kelly Kearney
Kelly Kearney 2015년 11월 4일
편집: Kelly Kearney 2015년 11월 5일
A second option would be to layer two axes manually. For example:
x = rand(100,1);
fac = 2.54;
ax(1) = axes('box', 'off');
ax(2) = axes('Position', get(ax(1), 'Position'), 'yaxislocation', 'right', ...
'xaxislocation', 'top', 'box', 'off', 'color', 'none');
hold(ax(1), 'on');
plot(ax(1), x);
ylabel(ax(1), 'Distance (in)');
ylabel(ax(2), 'Distance (cm)');
set(ax(1), 'ylim', [0 1]);
set(ax(2), 'ylim', get(ax(2), 'ylim')*fac);
The axis layering is basically the same thing plotyy does, except that plotyy chooses axis limits and tick intervals designed to align the tick marks on the two opposing axes. My example doesn't do this (which is why you need to set the 'Box' property to 'off' on both axes, or you'll get some spurious tick marks).

카테고리

Help CenterFile Exchange에서 Two y-axis에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by