Combine LineStyleOrder and ColorOrder

8 次查看(过去 30 天)
Hello all,
I have a dataset which I want to plot by looping through a specific set of colours and linestyles at the same time. My problem is that while the colours are looping through, the linestyles do not. Does anyone have an idea of what I'm doing wrong?
y2 = randi([10 50],14,12);
set(gcf,'DefaultAxesColorOrder',[0,0,1; ...
0,0.7,0.3; ...
0.5,0,1; ...
0,0,0; ...
0.6,0.6,0.6; ...
0,0,0; ...
0,0.75,0.75; ...
0.9,0.4,0; ...
0.9,0,0.1; ...
1,0.6,0; ...
0.3,1,0.3], ...
'DefaultAxesLineStyleOrder',{'-o','-','-.','-x','-+','--+','--','-.','-+','-o','-o'});
ax = gca;
for i = 1:size(y2,2)
plot(x,y2(:,2:end));
ax.LineStyleOrderIndex = ax.ColorOrderIndex;
end

采纳的回答

Image Analyst
Image Analyst 2021-9-26
If that didn't work then perhaps try it like this instead.
y2 = randi([10 50],11,11);
x = 1 : length(y2);
myColors = [0,0,1; ...
0,0.7,0.3; ...
0.5,0,1; ...
0,0,0; ...
0.6,0.6,0.6; ...
0,0,0; ...
0,0.75,0.75; ...
0.9,0.4,0; ...
0.9,0,0.1; ...
1,0.6,0; ...
0.3,1,0.3];
lineStyles = {'-','-','-','-','-','--','--','-','-','-','-'};
markerStyles = {'o','x','.','x','+','+','.','.','+','o','o'};
ax = gca;
for k = 1:size(y2,2)
thisColor = myColors(k, :)
thisLineStyle = lineStyles{k};
thisMarkerStyle = markerStyles{k};
plot(x,y2(k, :), 'Color', thisColor, ...
'LineStyle', thisLineStyle, ...
'Marker', thisMarkerStyle, ...
'LineWidth', 3, 'MarkerSize', 18);
hold on;
end
g = gcf;
g.WindowState = 'maximized'

更多回答(1 个)

Adam Danz
Adam Danz 2023-3-17
Starting in MATLAB R2023a you can cycle through LineStyleOrder and ColorOrder together by setting LineStyleCyclingMethod to 'withcolor'.
Demo
styles = {'-','-o', '-^'};
colors = [1 0 0; 0 0 1; 0 1 0];
ax = axes();
ax.LineStyleOrder = styles;
ax.ColorOrder = colors;
ax.LineStyleCyclingMethod = 'withcolor';
hold on
plot(0:.1:1, rand(1,11)+(1:6)')
legend

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by