specify grey color in two plots that share same x axis

11 次查看(过去 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!

采纳的回答

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 中查找有关 Annotations 的更多信息

标签

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by