필터 지우기
필터 지우기

How can I define my own colororder for a bodeplot?

조회 수: 75 (최근 30일)
Anna-Maria
Anna-Maria 2014년 8월 19일
댓글: Renan Geraldes 2022년 2월 1일
Hi, I would like to plot up to 20 bodeplots in the same graph with a specified colororder. I have tried two different ways of doing this, but none of them works:
1) figure; color_order = copper(20); set(gcf,'DefaultAxesColorOrder',color_order) hold all for i = 1:20 bodeplot(models{i}); end
I have also tried by setting the default colororder for the whole matlab session with set(0,'DefaultAxesColorOrder',color_order), but this doesn't affect the color order for the bodeplot either.
2) figure;hold; color_order = copper(20); for i = 1:20 bodeplot(models{i},color_order(i)); end
Nr 2 does not work since bodeplot does not seem to take rgb-colors. I have also tried
figure;hold; color_order = copper(20); for i = 1:20 bodeplot(models{i},'Color',color_order(i)); end
Cheers, Anna-Maria

채택된 답변

Aniruddha Katre
Aniruddha Katre 2014년 8월 19일
The function "bodeplot" defaults to colors specified in MATLAB. If you try to set a color order when using the "hold on" command, for each new plot, MATLAB uses the first value in the color order. In order to work around this, you will need to set the line colors to custom RGB values using the handles associated with the line objects. First you will have to obtain these handles associated with the line plots and then set their 'Color' property. You can find the handles associated with each line on the plot using the command:
lineHandle = findobj(gcf,'Type','line','-and','Color','b');
Here, the function "findobj" looks for objects in the figure of type "line" and color "b", and then obtains their handles.
Once you have these handles, you can then change the line color as per your requirements. You can do this by using the command:
set(lineHandle,'Color',col(ii,:));
The variable "col" contains the color order defined by "copper".
For more information on "findobj" please see: findobj
Here's a sample version of the code you want to execute:
% Define the models
sys{1} = tf([1],[2 1]) % Define one system
sys{2} = tf([2],[2 3 1]) % Define another system
% Define the color order based on the number of models
col = copper(numel(sys)); %
% Set the color to the color order you want
for ii = 1:numel(sys)
% Default the new plot color to blue
bodeplot(sys{ii},'b');
% Find handles of all lines in the figure that have the color blue
lineHandle = findobj(gcf,'Type','line','-and','Color','b');
% Change the color to the one you defined
set(lineHandle,'Color',col(ii,:));
% Hold on
hold on
end
% Hold off
hold off;
  댓글 수: 2
Danielius Kramnik
Danielius Kramnik 2020년 6월 9일
This doesn't update the colors in the legend for me.
Felix Menzel
Felix Menzel 2020년 6월 15일
The legend function requires the line handles to set the correct colors. (last line in example)
f_test = figure;
hold on
% plotting
for ii = 1:length(Geschw)
bodeplot(logg.(v_str{ii}).sys);
end
% find all Axes of subplots
h_axes = findobj(gcf,'Type','Axes');
for ii = 1:length(h_axes)
% find all lines in current Axes
h_lines = findobj(h_axes(ii),'Type','line');
% set grid
set(h_axes(ii),'XGrid','on')
set(h_axes(ii),'YGrid','on')
% loop over lines to set color
for jj = 1:length(h_lines)
h_lines(jj).Color = cmap(jj,:);
end
end
% make legend
legend(h_lines,Geschw_str)

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Scott Tatum
Scott Tatum 2019년 1월 8일
So I know this is a bit of an older question but just came across it myself. After looking through a few answers I found the following (in 2017b):
h = bodeplot(sys,w,options);
h.Responses(n).Style.Colors{1} = cols(n,:); % set the color
h.Responses(n).Style.LineStyles{1} = stys{n}; % set the line style
h.Responses(n).Style.setstyle % make the changes show
This allowed me to set custom colors and line styles progromatically for each sys that I plotted on a figure.

Honglei Chen
Honglei Chen 2014년 8월 19일
You can define your own colormap using colormapeditor

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by