How can I plot a bode diagram with random color?
古いコメントを表示
What should I add to this code to have a random color?
sys=tf(1,[1 1])
bode(sys)
1 件のコメント
Paulo Silva
2011 年 6 月 21 日
Please always provide detailed questions so we can help you better and faster
採用された回答
その他の回答 (4 件)
Chirag Gupta
2011 年 6 月 21 日
colors =['r','b','g','y','m','k'];
c = ceil(6*(rand))
bode(sys,colors(c))
This will pseudo randomly choose one of the 6 colors. You can obviously add more colors to the list!
1 件のコメント
Chirag Gupta
2011 年 6 月 21 日
MATLAB figures default to a particular color. Unless you change that setting or change the colors via code, you will get the same color.
Walter Roberson
2011 年 6 月 21 日
Provided that your systems are not MIMO,
sys=tf(1,[1 1]);
h = bodeplot(sys); %notice this is _not_ bode()
for L = findobj(h,'type','line')
set(L,'Color', rand(1,3));
end
Note: this does not attempt to match colors between the frequency and phase components. You did ask for random color...
I do not have the appropriate toolbox to probe to determine what would be needed to use the same color between the two halves when multiple systems are being drawn on the same plot, or when bode() is used to draw an array of bode plots for a MIMO system.
4 件のコメント
Walter Roberson
2011 年 6 月 21 日
sys=tf(1,[1 1]);
h = bodeplot(sys); %notice this is _not_ bode()
set(findobj(h,'type','line'),'Color', rand(1,3));
Not valid for MIMO systems or for specifying multiple systems to be plotted simultaneously.
Paulo Silva
2011 年 6 月 21 日
Walter your code allows similar colors on the same axes, would be better to use hold all or make a random list of colors so each new bode plot would use just one color from the list (until the last color and it returns to the first of the list)
Walter Roberson
2011 年 6 月 21 日
If you cannot have similar or duplicate colors, then the colors have not been chosen at random as requires by the original question ;-)
Paulo Silva
2011 年 6 月 21 日
Good point Walter :)
Niels
2013 年 5 月 2 日
1 投票
Here is a solution for giving bode plots a different color than and then 'ymcrgbk' and subsequently saving the figure without unwanted changes to the legend.
sys1=tf(1,[1 1]); sys2=tf(1,[1 1 1]);
figure; hold on;
bode(sys1,'b'); bode(sys2,'r');
h1 = findobj(gcf,'Color','b','-and','linestyle','-'); set(h1,'linewidth',1,'color',[.9 .9 .9]);
h2 = findobj(gcf,'Color','r','-and','linestyle','-'); set(h2,'linewidth',2,'color',[.5 .5 .5]);
legend([h1(1) h2(1)],'system 1','system 2')
It is important to assign the first term of the handle (e.g. h1(1)) in order to avoid issues with matrix dimentions (horzcat)
Chirag Gupta
2011 年 6 月 21 日
bode(sys,'r')
You can always write some code to pick a random color from a set of colors that you would like
カテゴリ
ヘルプ センター および File Exchange で Plot Customization についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!