specify grey color in two plots that share same x axis

조회 수: 8 (최근 30일)
Ines Shekhovtsov
Ines Shekhovtsov 2022년 12월 7일
댓글: Voss 2022년 12월 7일
Hello,
I have the following code to generate a plot:
plot(TimeVector,RPr,TimeVector,RP_rms,'r');
The RP_rms trace is red and by default the RPr trace is blue. I wish to change that color to grey. Since that doesn't have a letter assiciated with it, i tried using this combination but it didn't work:
greyColor= [.7 .7 .7];
plot(TimeVector,LPr,'Color', grayColor,TimeVector,LP_rms,'r');
I am aware that i can accomplish what i need with hold on and hold off:
gray = [.7 .7 .7];
plot(TimeVector,LPr,'Color', gray)
hold on
plot(TimeVector,LP_rms, 'r')
hold off
However, i am just curious if it can all be accomplished on the same line of code as i tried in the beginning.
Thank you for your help in advance!

채택된 답변

Voss
Voss 2022년 12월 7일
There is a way to do that in one line of code:
TimeVector = 0:10;
LPr = [1:6 5:-1:1];
LP_rms = 3*ones(1,11);
set(subsref(plot(TimeVector,LPr,TimeVector,LP_rms,'r'),substruct('()',{1})),'Color',[0.7 0.7 0.7]);
This is plotting the two lines in red, capturing their handles returned from plot, then setting the color of the first one to grey ([0.7 0.7 0.7]).
  댓글 수: 2
Ines Shekhovtsov
Ines Shekhovtsov 2022년 12월 7일
Thank you! Is one way more efficient/ideal than the other?? Using your method vs hold on and hold off??? Just curious.
Thanks again!
Voss
Voss 2022년 12월 7일
The one-liner above is equivalent to:
h = plot(TimeVector,LPr,TimeVector,LP_rms,'r');
set(h(1),'Color',[0.7 0.7 0.7]);
(except that this makes a variable "h" in the workspace).
I wouldn't expect hardly any difference in performance between this way, the subsref/substruct way and the hold on/hold off way.
Therefore, the way I would do it would be the way that has the clearest code, which rules out the subsref/substruct one-liner, in my opinion.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Deep Learning Toolbox에 대해 자세히 알아보기

태그

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by