How do we define the colors in the MarkerFaceColor for the plots containing lines and points?

137 views (last 30 days)
I am plotting a graph where I have nine speakers (see the attached). I am not sure why the color of S8_M is the same as S1_F, and S9_F is the same as S2_F. This is confusing as we can't differentiate the speakers' data. May I know how to define the colors and not use the default settings? Or where am I doing wrong? Any suggestions will be appreciated. Part of the code is given below:
ax(1)=subplot(1,2,1);
h1 = plot(x1*100, y1, '-o');
set(h1, {'MarkerFaceColor'}, get(h1,'Color'));
axis padded
grid;
xlabel('Time points (%)');
ylabel('f0 (Hz)');
title('beer');
linkaxes(ax,'y')
ax(2) =subplot(1,2,2);
h2 = plot(x2*100, y2, '-o');
set(h2, {'MarkerFaceColor'}, get(h2,'Color'));
title('north');
axis padded
grid;

Accepted Answer

Voss
Voss on 2 Apr 2022
Edited: Voss on 2 Apr 2022
Inspect either of the properties shown here:
get(0,'DefaultAxesColorOrder') % default line colors for any axes
ans = 7×3
0 0.4470 0.7410 0.8500 0.3250 0.0980 0.9290 0.6940 0.1250 0.4940 0.1840 0.5560 0.4660 0.6740 0.1880 0.3010 0.7450 0.9330 0.6350 0.0780 0.1840
figure('Visible','off');
get(gca(),'ColorOrder') % this particular axes' line colors (same as above by default)
ans = 7×3
0 0.4470 0.7410 0.8500 0.3250 0.0980 0.9290 0.6940 0.1250 0.4940 0.1840 0.5560 0.4660 0.6740 0.1880 0.3010 0.7450 0.9330 0.6350 0.0780 0.1840
You see there are 7 colors defined (7 rows of RGB values). MATLAB cycles through these colors when you use plot and other high-level plotting functions. Since there are 7 colors there and you plot 9 lines, the last two lines get the same colors as the first two, as you observe.
There are different ways to go about changing this. One way is to change one or both of the above settings:
% add grey and black to the default set of colors for any axes:
default_colors = get(0,'DefaultAxesColorOrder');
set(0,'DefaultAxesColorOrder',[default_colors; 0.5 0.5 0.5; 0 0 0]);
% or add grey and black to the set of colors for this axes only:
default_colors = get(gca(),'ColorOrder');
set(gca(),'ColorOrder',[default_colors; 0.5 0.5 0.5; 0 0 0]);
Then use plot like before:
set(gcf(),'Visible','on');
for ii = 1:9
plot(1:10,-ii+(1:10)); % using color order with 9 colors now, last two are grey and black
hold on
end
legend();
Or you can explicitly specify each color when you plot, without changing those settings:
figure();
colors = rand(12,3); % 12 random colors, you can specify the colors you want here
for ii = 1:size(colors,1)
plot(1:10,-ii+(1:10),'Color',colors(ii,:));
hold on
end
legend();
  4 Comments
Anu
Anu on 2 Apr 2022
Ohh makes sense! Thanks. It works perfectly.
ax(1)=subplot(1,2,1);
default_colors = get(gca(),'ColorOrder');
default_colors = [default_colors; 0.5 0.5 0.5; 0 0 0];
set(gca(),'ColorOrder',default_colors);
h1 = plot(x1*100, y1, '-o');
set(ax(1),'ColorOrder',default_colors);
set(h1, {'MarkerFaceColor'}, get(h1,'Color'));
axis padded
grid;
xlabel('Time points (%)');
ylabel('f0 (Hz)');
title('beer');
linkaxes(ax,'y')
ax(2) =subplot(1,2,2);
h2 = plot(x2*100, y2, '-o');
set(ax(2),'ColorOrder',default_colors);
set(h2, {'MarkerFaceColor'}, get(h2,'Color'));
title('north');
axis padded
grid;

Sign in to comment.

More Answers (0)

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!