I would like to plot multiple lines with MATLAB and do it so, that markers would be different in every line. I know that with colours this would be achieved with ColorSet = hsv(12);
. Is there some as simple as this method for markers?
6 Answers
Well, I am not aware of a built-in functionality of MATLAB to do so, but I do the following. I create my own cell:
markers = {'+','o','*','.','x','s','d','^','v','>','<','p','h'}
and then access it this way:
markers{mod(i,numel(markers))+1}
I also created a function, getMarker
, that does that and that I added to the path of MATLAB so that I can access it in all my scripts.

- 30,738
- 21
- 105
- 131

- 6,038
- 4
- 22
- 37
-
This looks very similar as my solution. But thanks for remembering me about the possiblity of making functions. – GC87 Mar 07 '11 at 19:12
-
2Does anyone know if this is possible by just calling plot one time? – patrik Jan 09 '14 at 09:36
-
I've asked a question about passing in an array of line styles here: http://stackoverflow.com/questions/40058490/how-do-i-pass-an-array-of-line-specifications-or-styles-to-plot – Aralox Oct 15 '16 at 11:33
x = linspace(0, 2*pi);
y = cos(bsxfun(@plus, x(1:15:end), x'));
figure
m = {'+','o','*','.','x','s','d','^','v','>','<','p','h'};
set(gca(), 'LineStyleOrder',m, 'ColorOrder',[0 0 0], 'NextPlot','replacechildren')
plot(x, y)

- 476
- 5
- 11
-
Thanks, for the answer, it works. But, if i change the ColorOrder to [1 0 0; 0 1 0; 0 0 1] it does not work any more. Do you know why? – Derzu Oct 15 '14 at 02:52
-
AFAIK you can set either, not both, of LineStyleOrder and ColorOrder. – Felipe G. Nievinski Dec 17 '14 at 01:13
Yes, there's a ready made method: it's the LineStyleOrder axis property. To activate it you have to disable the ColorOrder property, which takes precedence over the former and is activated by default. You can do as follows:
m = {'+','o','*','.','x','s','d','^','v','>','<','p','h'};
set_marker_order = @() set(gca(), ...
'LineStyleOrder',m, 'ColorOrder',[0 0 0], ...
'NextPlot','replacechildren');
where the m
values were obtained manually from the output of help plot
.
Then use it as in this example:
x = linspace(0, 2*pi);
y = cos(bsxfun(@plus, x(1:15:end), x'));
figure
set_marker_order()
plot(x, y)

- 476
- 5
- 11
-
Thanks, for the answer, it works. But, if i change the ColorOrder to [1 0 0; 0 1 0; 0 0 1] it does not work any more. Do you know why? – Derzu Oct 15 '14 at 02:52
The following also helps.
function testfig
x=0:0.1:10;
y1=sin(x);
y2=cos(x);
m = ['h','o','*','.','x','s','d','^','v','>','<','p','h'];
plot(x,y1,[m(1)])
hold on;
plot(x,y2,[m(2)])

- 21
- 2
I am using a simple procedure to randomly create new styles for plots. Though it is not really an iteration but someone may find it useful:
function [styleString] = GetRandomLineStyleForPlot()
% This function creates the random style for your plot
% Colors iterate over all colors except for white one
markers = {'+','o','*','.','x','s','d','^','v','>','<','p','h'};
lineStyles = {'-', '--', ':', '-.'};
colors = {'y', 'm', 'c', 'r', 'g', 'b', 'k'};
styleString = strcat(markers(randi(length(markers), 1) ), ...
lineStyles(randi(length(lineStyles), 1) ), ...
colors(randi(length(colors), 1) ) );
end

- 21
- 1
The easiest way, assuming you are using plot
, is to add the type of line in the command.
Some of the possible options are: --
,:
,-
,-.
. There also options for the marker type and for the width.
For example this code will generate several lines with different types of markers:
x = -pi:.1:pi;
y = sin(x);
z = cos(x);
t = tan(x);
l = x.^2;
figure();
hold on;
plot (x,y,'--g');
plot (x,z,'-.y');
plot (x,t,'-b');
plot (x,l,':r');
hold off;
the generated graph is:
for more help go to: http://www.mathworks.com/help/techdoc/ref/linespec.html

- 8,745
- 28
- 84
- 130
-
Yes, I am using plot and I'm also familiar with this method you described. I however hope there would be some more general approach. I generate my plot in for-loop and set handle to it. I have managed to get right looking solution with table consisting possible markers and then using `set(h,'Marker',markers(j));` and keeping in mind that index j is smaller than markers table's size. But is there any ready build method to do this. – GC87 Mar 06 '11 at 16:24