specify grey color in two plots that share same x axis

6 Ansichten (letzte 30 Tage)
Ines Shekhovtsov
Ines Shekhovtsov am 7 Dez. 2022
Kommentiert: Voss am 7 Dez. 2022
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!

Akzeptierte Antwort

Voss
Voss am 7 Dez. 2022
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 Kommentare
Ines Shekhovtsov
Ines Shekhovtsov am 7 Dez. 2022
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 am 7 Dez. 2022
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by