Clear Filters
Clear Filters

changing the style in a loop with hold all...

25 views (last 30 days)
In a loop, "hold all" successively changes the colour of the lines in a plot...
But, this is not so useful to me because:
1. It goes back to a blue line after 7 plots.
2. I am going to be printing in black and white (or grey).
I would much rather Matlab rotated between the line-styles (-, --, :, ., etc...). Is there any way to change the style, as opposed the colour?
Note: I do not want a "just create an array of strings for the styles" solution, I am aware of this solution, and it feels wrong.
D Howard
  1 Comment
Ali on 29 Oct 2017
if true
--------------------------------------------------- code start
Input is "Input_Data", two dimension matrix
Markers = {'+','o','*','x','v','d','^','s','>','<'};
for i=1:10:size(Input_Data,1)
hold on
plot([0.5 1],[0.5 1],'--');
legend('Minpts = 100','Minpts = 200','Minpts = 300','Minpts = 400','Minpts = 500','Minpts = 600','Minpts = 700','Minpts = 800','Minpts = 900','Minpts = 1000','','Location','SouthEast');
xlabel('FPR or (1-Specificity)','FontSize',12,'FontWeight','bold'); ylabel('TPR or Spensitivity)','FontSize',12,'FontWeight','bold');
title('ROC Space');
-------------------------------------------- code end
--------------------------------------- picture link preview

Sign in to comment.

Accepted Answer

Kevin Holst
Kevin Holst on 21 Feb 2012
I found a solution to this, which I just put in your previous question.
If you set the default colororder and linestyleorder, I think it'll do what you want.
The commands are:
set(0,'defaultaxescolororder',[0 0 0; 0.5 0.5 0.5]) %black and gray
set(0,'defaultaxeslinestyleorder',{'-*',':','o'}) %or whatever you want
Patrick Kalita
Patrick Kalita on 22 Feb 2012
Without restarting MATLAB you can undo a default set like this:
% set a default:
set(0, 'DefaultAxesColorOrder', [0 0 0]);
% undo it:
set(0, 'DefaultAxesColorOrder', 'remove')
Kevin Holst
Kevin Holst on 23 Feb 2012
Thanks for the info, Patrick, that's definitely good to know.

Sign in to comment.

More Answers (4)

Jarrod Rivituso
Jarrod Rivituso on 21 Feb 2012
Ok I understand your remark about it "feeling wrong" so please don't hate me for suggesting this... However, a little-known trick is to use the set function to get a list of all possible options for things like markers or line styles. Example
ph = plot(1:10)
allLineStyles = set(ph,'LineStyle')
allMarkers = set(ph,'Marker')
You could then keep a counter and loop through a combination of those two properties. This way you aren't hard-coding a list of strings. It's still not as clean a solution as something like hold.
I'm not sure if there is a more elegant solution.

Patrick Kalita
Patrick Kalita on 21 Feb 2012
This is the same basic idea as Kevin Holst's answer, but you don't need to set the default LineStyleOrder for all axes. You can just set it for the one you're interested in:
% Set axes properties
set(gca, 'ColorOrder', [0 0 0; 0.5 0.5 0.5]); % just black and grey
set(gca, 'LineStyleOrder', {'-', ':', '--', '-.'}); % different line styles
% Call hold so that the first plot doesn't affect the properties you just set
hold all
% Now make some plots
plot( rand( 1, 10 ) );
plot( rand( 1, 10 ) );
plot( rand( 1, 10 ) );
plot( rand( 1, 10 ) );
plot( rand( 1, 10 ) );
plot( rand( 1, 10 ) );
Also note that MATLAB uses each color in the ColorOrder with the first line style in LineStyleOrder, then it uses each color with the second style in LineStyleOrder, etc.
  1 Comment
Hems on 8 Feb 2018
Is there any way to modify the code such that Matlab uses each color in the ColorOrder with all the line styles in LineStyleOrder and then jumps to the next color?

Sign in to comment.

Edward Byers
Edward Byers on 23 May 2013
I have been battling with this exact problem and in finding your question, found the answer, and suggest a similar solution that you may like.
mrk={'o','s','*','v','+','^'}; %These are the markers
for n=1:6
hold all %The position of hold all BEFORE 'set(gca)' and 'plot' is important
set(gca,'LineStyle',mrk(n)) % mrk(n) will cycle through markers in your loop

Bruno Melo
Bruno Melo on 20 Jun 2019
I've also read all forum questions concerning this issue and got around by using this function made by Sébastien Martin.
Hope it helps you.

Community Treasure Hunt

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

Start Hunting!