How to draw yline on yyaxis on right hand axis?

12 views (last 30 days)
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.
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")
ylabel("Irradiance [W/m^2]")
grid on
%yline(mean(T2_filtered.("Global solar irradiance"))) % this plots on left axis

Accepted Answer

Star Strider
Star Strider on 21 Jul 2023
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;
yyaxis right
plot(x, y2)
yline(2*pi,'--') % Plots Correctly W.R.T. Right 'yyaxis'
yyaxis left
plot(x, y1)
arjun luther
arjun luther on 22 Jul 2023
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 on 23 Jul 2023
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.

Sign in to comment.

More Answers (0)


Find more on Weather and Atmospheric Science in Help Center and File Exchange




Community Treasure Hunt

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

Start Hunting!