You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
How to draw yline on yyaxis on right hand axis?
19 views (last 30 days)
Show older comments
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
Accepted Answer
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)
.
12 Comments
arjun luther
on 21 Jul 2023
Hi, thanks for your answer. I have tried to upload the matlab table in the question, maybe you can try with the actual data , if the code works for you.
Star Strider
on 22 Jul 2023
My pleasure!
Use the paperclip icon on the top toolstrip to upload it here. I very rarely go to offline sites to get files.
arjun luther
on 22 Jul 2023
i tried that already but the table is more than 5 mb, so i had to put it on drive to upload it here. Even compressed version was more than 5 mb. Any other ideas, how to share here directly if not via google drive.
Star Strider
on 22 Jul 2023
When I try accessing it, I get:
Google Drive can't scan this file for viruses.
This file is executable and may harm your computer.
Post a safe version, and the code to import it.
.
arjun luther
on 22 Jul 2023
i converted the table to .txt and format and had to compress it twice. Hope fully it can work this time.
Star Strider
on 22 Jul 2023
That works, and the yline call works correctly as well.
I checked it by creating the ‘Check_Value’ variable, and it plots where it should plot, at 460.2 with respect to the right axis —
Ux = unzip('t2.1.zip')
Ux = 1×1 cell array
{'t2.txt'}
T2_filtered = readtable(Ux{1}, 'VariableNamingRule','preserve')
T2_filtered = 759012×16 table
Datetime Latitude Longitude Altitude Aircraft Roll Aircraft Pitch Heading Air Temperature Reflected shortwave Irradiance Reflected Longwave irradiance Global solar irradiance Emitted longwave irradiance Aircraft North velocity Aircraft East velocity Aircraft down velocity Ground speed
_______________________ ________ _________ ________ _____________ ______________ _______ _______________ ______________________________ _____________________________ _______________________ ___________________________ _______________________ ______________________ ______________________ ____________
2021-07-24T12:53:00.000 53.864 8.2083 96.2 0.4 3.4 78.4 21.4 37 420 750 335 11 59.3 0 70.3
2021-07-24T12:53:00.010 53.864 8.2083 96.2 0.4 3.4 78.4 21.4 37 420 749 335 11 59.3 0 70.3
2021-07-24T12:53:00.020 53.864 8.2083 96.2 0.4 3.4 78.4 21.5 37 420 749 335 11 59.3 0 70.3
2021-07-24T12:53:00.030 53.864 8.2083 96.2 0.4 3.4 78.4 21.4 37 420 749 335 11 59.3 0 70.3
2021-07-24T12:53:00.040 53.864 8.2083 96.2 0.4 3.4 78.4 21.5 37 420 749 335 11 59.3 0 70.3
2021-07-24T12:53:00.050 53.864 8.2083 96.2 0.4 3.4 78.4 21.5 37 420 749 335 11 59.3 0 70.3
2021-07-24T12:53:00.060 53.864 8.2083 96.2 0.4 3.4 78.4 21.5 37 420 749 335 11 59.3 0 70.3
2021-07-24T12:53:00.070 53.864 8.2083 96.2 0.4 3.4 78.4 21.5 37 420 749 335 11 59.3 0 70.3
2021-07-24T12:53:00.080 53.864 8.2083 96.2 0.4 3.4 78.4 21.4 37 420 749 335 11 59.3 0 70.3
2021-07-24T12:53:00.090 53.864 8.2084 96.2 0.4 3.4 78.4 21.4 37 420 749 335 11 59.3 0 70.3
2021-07-24T12:53:00.100 53.864 8.2084 96.2 0.4 3.4 78.4 21.4 37 420 749 335 11 59.3 0 70.3
2021-07-24T12:53:00.110 53.864 8.2084 96.2 0.4 3.4 78.4 21.5 37 420 749 335 11 59.3 0 70.3
2021-07-24T12:53:00.120 53.864 8.2084 96.2 0.4 3.4 78.4 21.4 37 420 749 335 11 59.3 0 70.3
2021-07-24T12:53:00.130 53.864 8.2084 96.2 0.4 3.4 78.4 21.5 37 420 749 335 11 59.3 0 70.3
2021-07-24T12:53:00.140 53.864 8.2084 96.2 0.4 3.4 78.4 21.4 37 420 749 335 11 59.3 0 70.3
2021-07-24T12:53:00.150 53.864 8.2084 96.2 0.4 3.4 78.4 21.5 37 420 749 335 11 59.3 0 70.3
f6=figure;
colororder({'green','red'})
yyaxis right
plot(T2_filtered,"Datetime","Global solar irradiance","LineStyle","-",'Marker','.','MarkerEdgeColor',rand(1,3))
hold on
ax=gca % tried with and without
ax =
Axes with properties:
YAxisLocation: 'right'
YAxis: [2×1 NumericRuler]
YLim: [100 800]
XLim: [Jul 24, 2021, 12:30 Jul 24, 2021, 17:00]
XScale: 'linear'
YScale: 'linear'
Position: [0.1300 0.1100 0.7750 0.8150]
Units: 'normalized'
Show all properties
Check_Value = mean(T2_filtered.("Global solar irradiance")) % Delete
Check_Value = 460.3263
yline(mean(T2_filtered.("Global solar irradiance")))% also tried , calaculating mean beforehand
plot(T2_filtered,"Datetime","Emitted longwave irradiance","LineStyle","-","Marker",'.','MarkerEdgeColor',rand(1,3))
ylabel("Irradiance [W/m^2]")
yyaxis left
plot(T2_filtered,"Datetime","Reflected shortwave Irradiance",'LineStyle','-','Marker','.','MarkerEdgeColor',rand(1,3))
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
I have no idea what ‘rgb’ is, so I just created random vectors for those calls.
.
arjun luther
on 22 Jul 2023
That is so strange. I have tried it on both online as well offline versioin of Matlab, for me it only plots on left hand axis. Rgb is an addon from climate tool box that helps defining colour which are different from basic matlab pre defined colors. I used it as experiment before, never changed the code back.
Any other suggestions, what can i do about the main problem.
Star Strider
on 22 Jul 2023
I searched the online documentation and could not find any reference to ‘rgb’ and I have never heard of the Climate Toolbox. (I assume it must be a FEX contribution.)
The only conclusion I can come up with is that the Climate Toolbox itself may be causing problems with the yline call, since it works correctly here. It may have its own version of yyaxis or yline, or it may tweak it in some way. (You might also mention this as a comment to the Climate Toolbox FEX page, since it may be a bug.)
I would just save the data (as you did here), close the Climate Toolbox, and plot it after that, since that works. As far as the ‘rgb’ call goes, just choose an appropriate colormap and index into it, or create your own matrix of colours.
arjun luther
on 22 Jul 2023
I tried, disabling the add on , but still same result. It's so annoying that such a simple task cannot be done. I m considering making vector of same size that contains the value of mean , say 460.2 and than plot it as normal line plot.
Star Strider
on 22 Jul 2023
The easiest way to do that would be:
plot(xlim,[1 1]*mean(T2_filtered.("Global solar irradiance")), '-k')
Adding that here just after the first hold call in yyaxis right —
Ux = unzip('t2.1.zip')
Ux = 1×1 cell array
{'t2.txt'}
T2_filtered = readtable(Ux{1}, 'VariableNamingRule','preserve')
T2_filtered = 759012×16 table
Datetime Latitude Longitude Altitude Aircraft Roll Aircraft Pitch Heading Air Temperature Reflected shortwave Irradiance Reflected Longwave irradiance Global solar irradiance Emitted longwave irradiance Aircraft North velocity Aircraft East velocity Aircraft down velocity Ground speed
_______________________ ________ _________ ________ _____________ ______________ _______ _______________ ______________________________ _____________________________ _______________________ ___________________________ _______________________ ______________________ ______________________ ____________
2021-07-24T12:53:00.000 53.864 8.2083 96.2 0.4 3.4 78.4 21.4 37 420 750 335 11 59.3 0 70.3
2021-07-24T12:53:00.010 53.864 8.2083 96.2 0.4 3.4 78.4 21.4 37 420 749 335 11 59.3 0 70.3
2021-07-24T12:53:00.020 53.864 8.2083 96.2 0.4 3.4 78.4 21.5 37 420 749 335 11 59.3 0 70.3
2021-07-24T12:53:00.030 53.864 8.2083 96.2 0.4 3.4 78.4 21.4 37 420 749 335 11 59.3 0 70.3
2021-07-24T12:53:00.040 53.864 8.2083 96.2 0.4 3.4 78.4 21.5 37 420 749 335 11 59.3 0 70.3
2021-07-24T12:53:00.050 53.864 8.2083 96.2 0.4 3.4 78.4 21.5 37 420 749 335 11 59.3 0 70.3
2021-07-24T12:53:00.060 53.864 8.2083 96.2 0.4 3.4 78.4 21.5 37 420 749 335 11 59.3 0 70.3
2021-07-24T12:53:00.070 53.864 8.2083 96.2 0.4 3.4 78.4 21.5 37 420 749 335 11 59.3 0 70.3
2021-07-24T12:53:00.080 53.864 8.2083 96.2 0.4 3.4 78.4 21.4 37 420 749 335 11 59.3 0 70.3
2021-07-24T12:53:00.090 53.864 8.2084 96.2 0.4 3.4 78.4 21.4 37 420 749 335 11 59.3 0 70.3
2021-07-24T12:53:00.100 53.864 8.2084 96.2 0.4 3.4 78.4 21.4 37 420 749 335 11 59.3 0 70.3
2021-07-24T12:53:00.110 53.864 8.2084 96.2 0.4 3.4 78.4 21.5 37 420 749 335 11 59.3 0 70.3
2021-07-24T12:53:00.120 53.864 8.2084 96.2 0.4 3.4 78.4 21.4 37 420 749 335 11 59.3 0 70.3
2021-07-24T12:53:00.130 53.864 8.2084 96.2 0.4 3.4 78.4 21.5 37 420 749 335 11 59.3 0 70.3
2021-07-24T12:53:00.140 53.864 8.2084 96.2 0.4 3.4 78.4 21.4 37 420 749 335 11 59.3 0 70.3
2021-07-24T12:53:00.150 53.864 8.2084 96.2 0.4 3.4 78.4 21.5 37 420 749 335 11 59.3 0 70.3
f6=figure;
colororder({'green','red'})
yyaxis right
plot(T2_filtered,"Datetime","Global solar irradiance","LineStyle","-",'Marker','.','MarkerEdgeColor',rand(1,3))
hold on
ax=gca % tried with and without
ax =
Axes with properties:
YAxisLocation: 'right'
YAxis: [2×1 NumericRuler]
YLim: [100 800]
XLim: [Jul 24, 2021, 12:30 Jul 24, 2021, 17:00]
XScale: 'linear'
YScale: 'linear'
Position: [0.1300 0.1100 0.7750 0.8150]
Units: 'normalized'
Show all properties
Check_Value = mean(T2_filtered.("Global solar irradiance")) % Delete
Check_Value = 460.3263
plot(xlim,[1 1]*mean(T2_filtered.("Global solar irradiance")), '-k') % <— ADDED
% yline(mean(T2_filtered.("Global solar irradiance")))% also tried , calaculating mean beforehand
plot(T2_filtered,"Datetime","Emitted longwave irradiance","LineStyle","-","Marker",'.','MarkerEdgeColor',rand(1,3))
ylabel("Irradiance [W/m^2]")
yyaxis left
plot(T2_filtered,"Datetime","Reflected shortwave Irradiance",'LineStyle','-','Marker','.','MarkerEdgeColor',rand(1,3))
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
Thst should work, since the first plot call after the hold call after yyaxis right plotted correctly. (I generally use hold on and hold off together for each separate yyaxis call, although it may not make any significant difference.)
.
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
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.
More Answers (0)
See Also
Categories
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!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)