How can I plot with different markers, linestyles and colors in a loop?

264 vues (au cours des 30 derniers jours)
Eik Brüser
Eik Brüser le 18 Déc 2020
Modifié(e) : Adam Danz le 17 Mar 2023
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!

Réponse acceptée

Adam Danz
Adam Danz le 18 Déc 2020
Modifié(e) : Adam Danz le 17 Mar 2023
Ways to control color and line style
1. LineStyleCyclingMethod
In MATLAB R2023a or later, the LineStyleCyclingMethod property of axes allows you to control how to cycle through line styles and colors when adding objects to axes. Options include
This example combines 2 line styles with 3 colors
styles = {'-','-o'};
colors = [1 0 0; 0 0 1; 0 1 0];
figure()
tcl=tiledlayout(1,3);
ax1 = nexttile;
ax1.LineStyleOrder = styles;
ax1.ColorOrder = colors;
ax1.LineStyleCyclingMethod = 'aftercolor';
hold on
plot(0:.1:1, rand(1,11)+(1:6)')
legend
title('aftercolor')
ax2 = nexttile;
ax2.LineStyleOrder = styles;
ax2.ColorOrder = colors;
ax2.LineStyleCyclingMethod = 'beforecolor';
hold on
plot(0:.1:1, rand(1,11)+(1:6)')
legend
title('beforecolor')
ax3 = nexttile;
ax3.LineStyleOrder = styles;
ax3.ColorOrder = colors;
ax3.LineStyleCyclingMethod = 'withcolor';
hold on
plot(0:.1:1, rand(1,11)+(1:6)')
legend
title('withcolor')
title(tcl, 'LineStyleCyclingMethod')
subtitle(tcl,'R2023a')
2. Use LineStyleOrder and ColorOrder
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.
3. Manually set line and color properties
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')
4 Manually set line and color properties
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
  1 commentaire
Eik Brüser
Eik Brüser le 22 Déc 2020
Thank you very much for your help. This is exactly what I'm looking for!
Happy holidays

Connectez-vous pour commenter.

Plus de réponses (1)

Marco Riani
Marco Riani le 18 Déc 2020
Please let me know if the code below helps
clear
close all;
clc;
n = (1:6);
F = rand(7,6);
figure
Markers={'o' '+' '*' '.' 'x' '_' '|' };
Colors={'r' 'g' 'b' 'c' 'm' 'y' 'k'};
LineTypes={'-' '--' ':' '-.' '-' '--' ':'};
hold on
for j =1:7
plot(n,F(j,:),'Marker',Markers{j},'Color',Colors{j},'LineStyle',LineTypes{j})
end
  1 commentaire
Eik Brüser
Eik Brüser le 22 Déc 2020
Thanks for your help! I've chosen the lineprops file by Adam. It's works fine.

Connectez-vous pour commenter.

Catégories

En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange

Produits


Version

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by