이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
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
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
2023년 7월 21일
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
2023년 7월 22일
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
2023년 7월 22일
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
2023년 7월 22일
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
2023년 7월 22일
i converted the table to .txt and format and had to compress it twice. Hope fully it can work this time.
Star Strider
2023년 7월 22일
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
2023년 7월 22일
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
2023년 7월 22일
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
2023년 7월 22일
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
2023년 7월 22일
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
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
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 Center 및 File Exchange에서 Weather and Atmospheric Science에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- 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)
아시아 태평양
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)