How can I define my own colororder for a bodeplot?
96 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Anna-Maria
il 19 Ago 2014
Commentato: Renan Geraldes
il 1 Feb 2022
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
0 Commenti
Risposta accettata
Aniruddha Katre
il 19 Ago 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".
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 Commenti
Felix Menzel
il 15 Giu 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)
Più risposte (2)
Scott Tatum
il 8 Gen 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.
Vedere anche
Categorie
Scopri di più su Plot Customization in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!