How to give different colors when I get stuck on a plot in MATLAB?

I have some data that say X with size (100.2). This X consists of data for 10 categories (set of 10). Now I would like to see the template in the data for each category. To do this, I need to have different colors for each category. I am trying to make a loop instead of 10 different graphs. I tried below.

hold on
for i=1:10:100
   plot(X(i:i+9,1),X(i:i+9,2),'.')
end
hold off

It gave me a plot of the same color. How to assign different colors for different ranges?

+4
source share
3 answers

The simplest solution is to replace hold onwith hold all.

, ( ), plot:

linespec = {'b.', 'r-', 'g--o'}; % define your ten linespecs in a cell array
hold on
for i=1:10:100
   plot(X(i:i+9,1),X(i:i+9,2),linespec{i})
end
hold off
+4

, hold all, , ColorOrder axes ( hold on hold all). MATLAB ( 7 R2013b) , , . 10 , , ColorOrder, N " " (GMPDC) MATLAB. :

, "", , RGB. , , ( ) .

, , 25:

25 "maximally perceptually-distinct colors"

GMPDC MathWorks - ( MATLAB, 7 ). MATLAB ColorOrder ,

distinguishable_colors(20)

, ColorOrder . , 10 " " 10 ( ColorOrder):

% Starting with X of size 100x2
X = reshape(X,10,10,2); % for clarity, column is category, row is observation
mpdc10 = distinguishable_colors(10) % 10x3 color list
hold on
for ii=1:10,
    plot(X(:,ii,1),X(:,ii,2),'.','Color',mpdc10(ii,:));
end

, ColorOrder :

X = reshape(X,10,10,2); % for clarity, and to avoid loop
mpdc10 = distinguishable_colors(10) % 10x3 color map
ha = axes; hold(ha,'on')
set(ha,'ColorOrder',mpdc10)
plot(X(:,:,1),X(:,:,2),'-.') % loop NOT needed, 'Color' NOT needed

ColorOrder RGB, ,

get(gca,'ColorOrder')

ColorOrder ,

get(0,'DefaultAxesColorOrder')

ColorOrder 10 MATLAB, startup.m:

set(0,'DefaultAxesColorOrder',distinguishable_colors(10))
+7

hold onensures that the new team plotadds to the schedule instead of replacing. However, each team works as if it creates a new story, including starting with the color of the first line (blue). If you want subsequent graphs to use different colors, use hold allinstead. Thus, standard 7-line colors are used in turn.

Since you have 10 lines to build, you can explicitly specify colors to make sure they are all different. To do this, use the syntax

plot(..., 'Color', [r g b])
+1
source

All Articles