How to draw yline on yyaxis on right hand axis?

19 次查看(过去 30 天)
Hi i am trying to draw a constant line at y axis in a plot for a mean. The issue is yline functions does not plot the yline on right hand axis, does not matter how i input the argument under yyaxis right but when i input the argument under yyaxis left, it plots. Maybe there is something wrong in my code, some can point it out to me.
f6=figure
colororder({'green','red'})
yyaxis right
plot(T2_filtered,"Datetime","Global solar irradiance","LineStyle","-",'Marker','.','MarkerEdgeColor',rgb('brown'))
hold on
ax=gca % tried with and without
yline(mean(T2_filtered.("Global solar irradiance")))% also tried , calaculating mean beforehand
plot(T2_filtered,"Datetime","Emitted longwave irradiance","LineStyle","-","Marker",'.','MarkerEdgeColor',rgb('black'))
ylabel("Irradiance [W/m^2]")
yyaxis left
plot(T2_filtered,"Datetime","Reflected shortwave Irradiance",'LineStyle','-','Marker','.','MarkerEdgeColor',rgb('orange'))
plot(T2_filtered,"Datetime","Reflected Longwave irradiance",'LineStyle','-','MarkerEdgeColor','cyan',"Marker",".")
% plot(T2_filtered, "Datetime", "Aircraft Roll",'Marker','.','MarkerEdgeColor',rgb('orange'))
% plot(T2_filtered, "Datetime", "Aircraft Pitch",'Marker','.','MarkerEdgeColor','g')
% plot(T2_filtered,"Datetime", "Altitude",'Marker','|','MarkerEdgeColor',rgb('silver'))
hold off
title("Short and Long Wave Irradiance")
xlabel("Time[hh:mm:ss]")
ylabel("Irradiance [W/m^2]")
legend(Location='northoutside',NumColumns=2)
grid on
%yline(mean(T2_filtered.("Global solar irradiance"))) % this plots on left axis

采纳的回答

Star Strider
Star Strider 2023-7-21
I don’t have your data, however in this simulation it seems to work correctly —
x = 0:10;
y1 = randn(size(x));
y2 = randn(size(x))*2+5;
figure
yyaxis right
plot(x, y2)
yline(2*pi,'--') % Plots Correctly W.R.T. Right 'yyaxis'
yyaxis left
plot(x, y1)
.
  12 个评论
arjun luther
arjun luther 2023-7-22
Thanks for your input as well as suggestions. It will get me going now. It would be really insightful if you could explain a little about this line of the code
"plot(xlim,[1 1]*mean(T2_filtered.("Global solar irradiance")), '-k')
what is happening here. How does multiplying the mean value with limits of x gets the trick done. Does xlim, [1 1] gets the whole x-axis? Thanks, once again
Star Strider
Star Strider 2023-7-23
As always, my pleasure!
The plot call uses the (1x2) xlim vector (defining the limits of the x-axis) as the independent variable argument, and the (1x2) vector [1 1] that is multiplied by the value of ‘mean(T2_filtered.("Global solar irradiance"))’ as the dependent variable argument, to produce a constant horizontal line at that value. Because it uses xlim as the independent variable argument, it will automatically re-size to span the entire x-axis if the x-axis limits are changed. It defines the line style as a constant black line using the '-k' argument.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Weather and Atmospheric Science 的更多信息

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by