How can I define my own colororder for a bodeplot?

100 views (last 30 days)
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

Accepted Answer

Aniruddha Katre
Aniruddha Katre on 19 Aug 2014
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 Comments
Danielius Kramnik
Danielius Kramnik on 9 Jun 2020
This doesn't update the colors in the legend for me.
Felix Menzel
Felix Menzel on 15 Jun 2020
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)

Sign in to comment.

More Answers (2)

Scott Tatum
Scott Tatum on 8 Jan 2019
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 on 19 Aug 2014
You can define your own colormap using colormapeditor

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!