cycling through both linestyle and linecolor

I want to plot lines that differ in both linestyle and linecolor (because if someone makes a b&w copy of the paper, they lose the linecolor information). If I specify a set of linestyles, plot cycles through all the colors on the first style, then all the colors on the second style, and so on. It would be really convenient if I could specify a default sequence of lines that include BOTH style and color; e.g.,
blue solid line
red dashed line
green dotted line
yellow dash-dot line, etc.
I want to be able to do this with Matlab's automatic line rendering, not plotting one line at a time, specifying the details for each one.
Does anyone know of a way to do this? Has anyone created a routine to do it?

 採用された回答

Adam Danz
Adam Danz 2023 年 3 月 17 日

1 投票

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
For solutions in MATLAB prior to R2023a see the other answers in this thread. Also see a summary of line style and color control in this answer.

その他の回答 (3 件)

Joseph Cheng
Joseph Cheng 2015 年 9 月 24 日
編集済み: Joseph Cheng 2015 年 9 月 24 日

1 投票

To answer the last question you can write your own plot function as i have below:
function hplot = cblindtest(pnts)
hplot = plot(pnts);
% styles:
% b blue . point - solid
% g green o circle : dotted
% r red x x-mark -. dashdot
% c cyan + plus -- dashed
% m magenta * star (none) no line
% y yellow s square
% k black d diamond
% w white v triangle (down)
% ^ triangle (up)
% < triangle (left)
% > triangle (right)
% p pentagram
% h hexagram
clor = 'bgrcmyk';
disp('debug')
linestyle={'-',':','-.','--'};
markertype = ' .ox+*sdv^<>ph';
mind = 1;
for ind = 0:length(hplot)-1
set(hplot(ind+1),'color',clor(mod(ind,7)+1));
lind = mod(ind,4);
set(hplot(ind+1),'linestyle',linestyle{lind+1});
if markertype(mind)==' '
mtype = 'none';
else
mtype = markertype(mind);
end
set(hplot(ind+1),'marker',mtype);
if mod(ind+1,4)==0;
mind = mod(mind,14)+1;
end
end
which can be run with
figure()
pnts = randi(6*13,1,1);
pnts = repmat(1:pnts,3,1);
cblindtest(pnts)
xlim([0 4])
edit as desired
pietro
pietro 2014 年 7 月 5 日
編集済み: pietro 2014 年 7 月 5 日

0 投票

you can do it by setting the line order style. Here an example:
set(gca,'linestyleorder',{'-',':','-.','--','*','+'},...
'colororder',[0 0 1;0 .5 0;1 0 0],'nextplot','add')
x = 0:.01:1;
plot(x,bsxfun(@power,x.',1:12))
in this example at first the '-' is set and then the colors are changed from blue to red for the first three lines. From the fourth to the sixth line the ':' styli is selected and the color will be changed. Hopefully it will helps

6 件のコメント

Hal Caswell
Hal Caswell 2014 年 7 月 5 日
This answer does not solve the problem. This is just the usual Matlab convention of first cycling through colors, and then changing the line style.
To be more clear about what I'm looking for, imagine that you plot just three lines using this approach. They will be blue, green, and red solid lines. If someone views them in black and white, they will appear identical.
I am looking to get, say, blue/solid, then green/dashed, then red/dotted.
pietro
pietro 2014 年 7 月 5 日
What about plotting all the in black as the follow?
set(gca,'linestyleorder',{'-',':','-.','--'}, 'colororder',[0 0 1],'nextplot','add')
pietro
pietro 2014 年 7 月 10 日
Please provide a feedback so we can help you
Christian
Christian 2015 年 9 月 24 日
編集済み: Christian 2015 年 9 月 24 日
Hi. I've got the same question. Has anybody found a solution? As Hal said, I want to plot three lines with the following attributes:
  1. Blue and solid
  2. Green and dashed
  3. Red and dotted
I know how to set those things manually, but I want this to be my default order.
Anton Fetzer
Anton Fetzer 2021 年 3 月 14 日
編集済み: Anton Fetzer 2021 年 3 月 14 日
@pietro Your code does not answer the question @Hal Caswell and @Christian have.
It changes the line style only after it has cycled through the colours.
But what they and myself want is to be able to cycle through line and colour options independently.
When I need to plot lines I want each of them to have a different colour and different line style.
No colour or linestyle used twice.
Your code uses the same line style until it runs out of colours.
That is the default behaviour that we do not want.
thomas greenhill
thomas greenhill 2021 年 4 月 28 日
Did you ever figure this one out?
If so, I would love to hear your solution.

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

Tongyao Pu
Tongyao Pu 2021 年 6 月 25 日
編集済み: Tongyao Pu 2021 年 6 月 25 日

0 投票

A semi-automatic code I have came up:
% define line style or color you want
sty = {'-', '--', '-.',':'};
col = {'#0072BD', '#D95319', '#EDB120', '#7E2F8E'};
% loop through your plot
for i = 1:4
plot(x, y, string(sty(i)), 'Color', string(col(i)))
% x, y can be vectors changin according to i
% ofc if i > 4, you can do something like mod(4, i) to make the index
% go back again.
end
It's essentially the same as Cheng's answer, but I made it this way so you can edit the color other than the bgrcmyk colors

カテゴリ

ヘルプ センター および File ExchangeData Distribution Plots についてさらに検索

製品

質問済み:

2014 年 7 月 5 日

回答済み:

2023 年 3 月 17 日

Community Treasure Hunt

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

Start Hunting!

Translated by