How can I plot the same data with two y-axes on the same plot?

조회 수: 74 (최근 30일)
Andrea
Andrea 2014년 6월 11일
댓글: Star Strider 2024년 2월 5일
I have some error analysis that requires looking at absolute as well as percent error of some data. I toyed around with plotyy but it essentially plots the data twice with each y-axis. Anyone know how to plot the data once with one scale on the left y-axis (absolute error) and another scale on the right (percent error)
Thanks!
  댓글 수: 1
Geoff Hayes
Geoff Hayes 2014년 6월 11일
편집: Geoff Hayes 2014년 6월 11일
Isn't that what plotyy is for? See the first example at http://www.mathworks.com/help/matlab/ref/plotyy.html as it plots "two data sets on one graph using two y-axes".

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

채택된 답변

Star Strider
Star Strider 2014년 6월 12일
This might do what you want:
x = 0:0.1:2*pi; % Create Data
y = 50 + 10*sin(x);
figure(1)
plotyy(x,y, x,y) % PlotYY
fcyy = get(gcf, 'Children') % Gets handles for both Y-axes
Ryax = fcyy(1); % Right axis handle
Lyax = fcyy(2); % Left axis handle
Lyaxt = get(fcyy(2), 'YTick') % Get Left axis ticks
LyaxDegC = round(10*(Lyaxt-32)/1.8)/10; % Convert to °C (or whatever you want)
set(Ryax, 'YTick',Lyaxt, 'YTickLabel', LyaxDegC) % New Y-tick values
I took a while for me to figure this out. It’s necessary to use ‘gcf’ to get the handles of the two Y-axes. Then, in order to put the right Y-axis ticks at the same places as the left axis ticks, do the conversion on the left axis ticks and then plot them on the right axis. Here, I did a °F to °C conversion. I don’t know how you want to calculate your percent errors, but the ‘LyaxDegC’ line (rename it and its reference in the following line) will do it. I decided to break it out into two lines rather than do it all in the ‘set’ line to make it easier to follow.
The plotyy documentation (that I didn’t think of reading until I completed this) suggests accessing the Y-axis values as:
[hAx,hLine1,hLine2] = plotyy(x,y1,x,y2);
then referring to ‘hAx(1)’ as the left axis handle and ‘hAx(2)’ as the right axis handle. That elinimates the ‘fcyy’ line in my code. The rest of my code remains unchanged.
  댓글 수: 6
Deik
Deik 2024년 2월 5일
I have stumbled upon the same question as @Andrea a while ago and found this answer.
But in this one it seems to be necessary to plot the data twice. Since my dataset is quite large that wouldn't work for me.
Is there a way to plot two y-axes (left and right) with a different scale for the same data? I have looked a lot but coudln't find anything so far.
Would be great if you had an idea.
Thanks!
Star Strider
Star Strider 2024년 2월 5일
@Deik — It turns out to be relatively srtaightforward to set a right axis scale without having to re-plot the data.
Try this —
x = linspace(0, 10);
y1 = sin(2*pi*x/2);
y2 = 10*(cos(2*pi*x/5) + 1);
figure
yyaxis left
plot(x, y1)
grid
yyaxis right
ylim([0 20])
figure
yyaxis left
plot(x, y1)
grid
yyaxis right
ylim([0 20])
Ax = gca;
axcolor = Ax.YAxis(1).Color;
Ax.YAxis(2).Color = axcolor;
The left axis color is set by the yyaxis function, however you can easily override that. The left y-axis is YAxis(1) and the right y-axis is YAxis(2). (I never needed to do that previously, so I needed to do a bit of experimentation.)
.

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

추가 답변 (3개)

Cedric
Cedric 2014년 6월 11일
편집: Cedric 2014년 6월 11일
You probably made a mistake, how are you calling PLOTYY?
doc plotyy
and look at examples, and e.g. the following which works
x = 0:0.01:20;
y1 = 200*exp(-0.05*x).*sin(x);
y2 = 0.8*exp(-0.5*x).*sin(10*x);
figure % new figure
plotyy(x,y1,x,y2);
  댓글 수: 2
Andrea
Andrea 2014년 6월 11일
I did something very similar to that, but the big difference is I'm looking at the exact same data for both y inputs, just on different scales. I'd like to see one set of data plotted, see the absolute error for a point on the left, and the percent error for the same point on the right. Is there maybe a different function or way to do this?
Cedric
Cedric 2014년 6월 11일
Could you give an example with a few data points, and provide the code that you are using for plotting?

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


Ranzige Erdnuss
Ranzige Erdnuss 2020년 9월 27일
편집: Ranzige Erdnuss 2020년 9월 27일
Hi
since plotyy is not recommended anymore, I made an similar approch for yyaxis:
ax = gca;
lLim=ax.YAxis(1).Limits;
rLim=lLim*conversionFaktorOrFun;
ax.YAxis(2).Limits=rLim;
ax.YAxis(1).Limits=lLim;
This following more complete script plots the new COVID-19 casee. Once per 100,000 residents on the left y-axis and once the total number on the right y-axis.
plot(dates, incidence, '.');
xlabel('date');
ylabel('daily new cases per 100,000 residents');
yyaxis('right');
plot(dates, incidence*population, '.', 'Color', 'none');
ylabel('total new infections');
ax = gca;
lLim=ax.YAxis(1).Limits;
rLim=lLim*population;
ax.YAxis(2).Limits=rLim;
ax.YAxis(1).Limits=lLim;
  댓글 수: 1
Star Strider
Star Strider 2020년 9월 27일
Thank you for your contribution.
Also, the original thread used R2014a, one release prior to R2014b, that introduced ‘HG2’, the new (and still current) handle graphics system. The original code will only work for release/version R2014a and those prior to it.

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


SAFDAR RASOOL
SAFDAR RASOOL 2019년 3월 11일
can we plot this double y plot with the simulink scope?

카테고리

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