How to draw yline on yyaxis on right hand axis?

12 views (last 30 days)
arjun luther on 21 Jul 2023
Commented: Star Strider on 23 Jul 2023
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
hold on
ax=gca % tried with and without
yline(mean(T2_filtered.("Global solar irradiance")))% also tried , calaculating mean beforehand
yyaxis left
% 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
xlabel("Time[hh:mm:ss]")
legend(Location='northoutside',NumColumns=2)
grid on
%yline(mean(T2_filtered.("Global solar irradiance"))) % this plots on left axis

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;
figure
yyaxis right
plot(x, y2)
yline(2*pi,'--') % Plots Correctly W.R.T. Right 'yyaxis'
yyaxis left
plot(x, y1)
.
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
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 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.

Categories

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

R2023a

Community Treasure Hunt

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

Start Hunting!