フィルターのクリア

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

159 ビュー (過去 30 日間)
Eik Brüser
Eik Brüser 2020 年 12 月 18 日
コメント済み: Peter 2024 年 6 月 25 日 13:52
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!

採用された回答

Adam Danz
Adam Danz 2020 年 12 月 18 日
編集済み: Adam Danz 2023 年 3 月 17 日
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
  2 件のコメント
Eik Brüser
Eik Brüser 2020 年 12 月 22 日
Thank you very much for your help. This is exactly what I'm looking for!
Happy holidays
Peter
Peter 2024 年 6 月 25 日 13:52
@Adam Danz thanks for the lineprops.m file. I think it would be worth adding this to the File Exchange.

サインインしてコメントする。

その他の回答 (1 件)

Marco Riani
Marco Riani 2020 年 12 月 18 日
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 件のコメント
Eik Brüser
Eik Brüser 2020 年 12 月 22 日
Thanks for your help! I've chosen the lineprops file by Adam. It's works fine.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeAnnotations についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by