Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reset ColorOrder index for plotting in Matlab / Octave

I have matrices x1, x2, ... containing variable number of row vectors. I do successive plots

figure
hold all % or hold on
plot(x1')
plot(x2')
plot(x3')

Matlab or octave normally iterates through ColorOrder and plot each line in different color. But I want each plot command to start again with the first color in colororder, so in default case the first vector from matrix should be blue, second in green, third in red etc.

Unfortunately I cannot find any property related to the color index niether another method to reset it.

like image 228
Honza Avatar asked May 12 '15 06:05

Honza


People also ask

How do I change the order of colors in Matlab?

When you set the color order for a figure, you set the color order for all the axes within that figure. colororder( target , newcolors ) sets the color order for the target axes, figure, or chart instead of the current figure. C = colororder returns the color order matrix for the current figure.

How do you set a color plot in Matlab?

Create a line plot and use the LineSpec option to specify a dashed green line with square markers. Use Name,Value pairs to specify the line width, marker size, and marker colors. Set the marker edge color to blue and set the marker face color using an RGB color value.


2 Answers

Starting from R2014b there's a simple way to restart your color order.

Insert this line every time you need to reset the color order.

set(gca,'ColorOrderIndex',1)

or

ax = gca;
ax.ColorOrderIndex = 1;

see: http://au.mathworks.com/help/matlab/graphics_transition/why-are-plot-lines-different-colors.html

like image 177
Yu-Heng Ian Ting Avatar answered Sep 20 '22 19:09

Yu-Heng Ian Ting


You can shift the original ColorOrder in current axes so that the new plot starts from the same color:

h=plot(x1');
set(gca, 'ColorOrder', circshift(get(gca, 'ColorOrder'), numel(h)))
plot(x2');

You can wrap it in a function:

function h=plotc(X, varargin)
h=plot(X, varargin{:});
set(gca, 'ColorOrder', circshift(get(gca, 'ColorOrder'), numel(h)));
if nargout==0,
    clear h
end
end

and call

hold all
plotc(x1')
plotc(x2')
plotc(x3')
like image 28
Mohsen Nosratinia Avatar answered Sep 19 '22 19:09

Mohsen Nosratinia