specify grey color in two plots that share same x axis
조회 수: 6 (최근 30일)
이전 댓글 표시
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!
댓글 수: 0
채택된 답변
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
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개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!