How to plot multiple lines with different markers
Asked Answered
G

7

9

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?

Gynecoid answered 6/3, 2011 at 12:39 Comment(0)
P
10

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.

Pea answered 6/3, 2011 at 16:39 Comment(3)
This looks very similar as my solution. But thanks for remembering me about the possiblity of making functions.Gynecoid
Does anyone know if this is possible by just calling plot one time?Presser
I've asked a question about passing in an array of line styles here: #40058990Leucopenia
C
4
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)
Celery answered 24/2, 2013 at 7:21 Comment(2)
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?Stefanistefania
AFAIK you can set either, not both, of LineStyleOrder and ColorOrder.Celery
C
3

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)
Celery answered 4/8, 2013 at 16:37 Comment(1)
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?Stefanistefania
E
2

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)])
Echeverria answered 17/12, 2013 at 10:45 Comment(0)
M
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
Mundford answered 25/8, 2015 at 8:31 Comment(0)
M
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: The yellow line is hard to spot, but it's there

for more help go to: http://www.mathworks.com/help/techdoc/ref/linespec.html

Mumbletypeg answered 6/3, 2011 at 16:3 Comment(1)
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.Gynecoid
F
0

As of MATLAB R2024a, you can use the function linestyleorder to define a set of line styles and markers.

For example,

Y = (10:15)'- (0:9);
plot(Y);
linestyleorder(["-", "--", "--x", ":."]);

gives

Figure with 10 lines, each with a different colour, and using various line styles and markers.


linestyleorder also comes with a set of predefined styles. For example,

Y = (10:15)'- (0:9);
plot(Y);
linestyleorder("mixedstyles");

gives

Figure with 10 lines, each with a different colour, and with different line patterns, but no markers.

and

Y = (10:15)'- (0:9);
plot(Y);
linestyleorder("mixedmarkers");

gives

Figure with 10 lines, each with a different colour, and with different markers, but not different line styles.

Fabulous answered 2/5 at 19:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.