yyaxis causing values on y-axes to become incorrect

조회 수: 7 (최근 30일)
Aditya Khuller
Aditya Khuller 2024년 3월 25일
댓글: Aditya Khuller 2024년 3월 25일
I am trying to create a plot with two y-axes. My goal is to show 4 lines, but two axes. The values of both axes are related,
I used yyaxis, but when I do, the values on the right y-axis are no longer correct. Can you please help me find the bug?
Here is the code I used:
figure;
yyaxis left
hold on;
xlabel('3-m Air Temperature (K)');
ylabel('Sublimation Rate (cm yr^{-1})');
plot(Theta2_warm,dzdt_DB_warm_high_wind,'k','LineWidth',3,'LineStyle','-');
plot(Theta2_warm,dzdt_warm_high_wind,'Color',[0.4940 0.1840 0.5560],'LineWidth',3,'LineStyle','-');
plot(Theta2_warm,dzdt_DB_warm_low_wind,'k','LineWidth',3,'LineStyle','--');
plot(Theta2_warm,dzdt_warm_low_wind,'Color',[0.4940 0.1840 0.5560],'LineWidth',3,'LineStyle','--');
ylim([0 4500])
set(gca,'YColor','k');
yyaxis right
ylabel('H_L (W m^{-2})')
plot(Theta2_warm,LE_DB_forced_warm_high_wind+LE_DB_free_warm_high_wind,'k','LineWidth',3,'LineStyle','none');
plot(Theta2_warm,LE_warm_high_wind,'Color',[0.4940 0.1840 0.5560],'LineWidth',3,'LineStyle','none');
plot(Theta2_warm,LE_DB_forced_warm_low_wind+LE_DB_free_warm_low_wind,'k','LineWidth',3,'LineStyle','none');
plot(Theta2_warm,LE_warm_low_wind,'Color',[0.4940 0.1840 0.5560],'LineWidth',3,'LineStyle','none');
set(gca,'YColor','k');
set(gca,'FontSize',18)
Here is the result:
Here is the result of the left y-axis, alone:
Here is the result of the right y-axis, alone:
At 240 K, the black and purple solid lines are ~1690 and 1620 for the right y-axis. But in the combined plot above, they are lower for some reason. I also tried removing the ylim statement, but the result is stil incorrect in the combined plot (the plot is showing that H_L is 2000 for some reason):
:

채택된 답변

Voss
Voss 2024년 3월 25일
The lines corresponding to the right y-axis are all plotted with LineStyle 'none' and no marker, so they don't appear at all. In other words, you are plotting 8 lines, but you only see 4, because the other 4 have no line and no markers.
  댓글 수: 5
Voss
Voss 2024년 3월 25일
@Adam Danz: Good idea, thanks!
Aditya Khuller
Aditya Khuller 2024년 3월 25일
Thank you both so much! I was able to adapt Adam's LimitsChangedFunction solution for my use to correct this issue :)

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Formatting and Annotation에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by