MATLAB: How to plot with different markers, linestyles and colors in a loop

colorcolororderlinestylelinestyleorderlinestyleorderindexloopmarkerMATLABseriesindex

I'm trying to plot frequency values from a loop over speed. I use multiple loops and it is not easy to plot with multiple markes, linestyles and colors. I found a function by Sébastien Martin in Matlab file exchange:
(See attachment)
It works if I try it with:
plot_styles(rand(10,6))
But as I need to plot with loops:
clear all,
close all;
clc;
n = (1:6);
F = rand(10,6);
figure
for j =1:10
plot_styles(n,F(j,:))
hold on
end
It did not give me different linestyles, colors and markers. Why? How can I make it work?
Thanks in advance!

Best Answer

There's no need for an independent function for this task.
Option 1
Set the LineStyleOrder and ColorOrder properties of the axes though be aware of some compatibility issues prior to Matlab r2019b. This demo enforces the same behavior in releases before and after r2019a. Note that all colors are used before cycling to the next marker.
fig = figure();
ax = axes(fig);
ax.LineStyleOrderIndex = ax.LineStyleOrderIndex; % [1]
ax.LineStyleOrder = {'-o','-+','-*','-x','-s','-d','-v','->','-h','-^'};
ax.ColorOrder = [1 0 0; 0 1 0; 0 0 1; 0 1 1; 1 0 1];
hold(ax,'on') % [2]
for i =1:10
plot(0:.1:1, rand(1,11)+i, 'DisplayName', ['Line ', num2str(i)]);
end
legend('Location','bestoutside')
% Footnotes
% [1] This line forces the axes to behave consistently between releases before and after r2019a (more info).
% [2] Important to preserve the property orders that were just set.
For additional flexibility if variation, see the SeriesIndex property of line objects.
Option 2
The demo shows how to list various markers, colors, and linestyles to be assigned to an infinite number of line objects. The properties are selected circularly so there is no requirement for the length of each property list. If the length of each property differ, you will have more combinations of markers, colors, and linestyles. Unlike option 1, the color, linestyle, and marker properties can vary on each iteration.
%% line properties
% List a bunch of markers; they will be selected in
% order and then the selection will start again if
% there are more lines than markers.
markers = {'o','+','*','s','d','v','>','h'};
% List a bunch of colors; like the markers, they
% will be selected circularly.
colors = {'b','g','r','k','c','m'};
% Same with line styles
linestyle = {'-','--','-.',':'};
% this function will do the circular selection
% Example: getprop(colors, 7) = 'b'
getFirst = @(v)v{1};
getprop = @(options, idx)getFirst(circshift(options,-idx+1));
%% Plot
figure()
hold on
for j =1:10
plot(0:.1:1, rand(1,11)+j,...
'Marker',getprop(markers,j),...
'color',getprop(colors,j),...
'linestyle',getprop(linestyle,j),...
'DisplayName', ['Line ', num2str(j)]);
end
legend('Location','bestoutside')
Option 3
The attached m-files contains a function lineprops(idx) that returns a set a marker, linestyle, and color name-value pairs based on the index value idx. There are 156 combinations.
Demo:
figure()
hold on
for i = 1:10
nameval = lineprops(i);
plot(0:.1:1,rand(1,11)+i, nameval{:}, 'LineWidth', 1.5)
end
legend('Location','BestOutside')
% lineprops() is available at
% https://www.mathworks.com/matlabcentral/answers/697655#answer_579560