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 Center および File ExchangeDeep Learning Toolbox についてさらに検索

タグ

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by