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

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.
Thanks
D Howard

1 Comment

if true
--------------------------------------------------- code start
Input is "Input_Data", two dimension matrix
Marker_Counter=1;
figure6=figure;
Markers = {'+','o','*','x','v','d','^','s','>','<'};
for i=1:10:size(Input_Data,1)
TPR=Input_Data(i:i+9,7);
FPR=Input_Data(i:i+9,8);
plot(FPR,TPR,strcat('-',Markers{Marker_Counter}));
Marker_Counter=Marker_Counter+1;
hold on
end
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');
close(gcf);
-------------------------------------------- code end
end
--------------------------------------- picture link preview
<</matlabcentral/answers/uploaded_files/92608/untitled.bmp>>

Sign in to comment.

 Accepted Answer

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

7 Comments

I like this better than my answer :)
Thanks Kevin,
I would like to understand this command a bit better, although I think I get the gist of it.
Do you have the link to where you found this solution?
D Howard
doc plot >> colororder or linestyleorder
If you're always going to use black and grey, you can set the defaults like I have in this answer. If you're going to be switching around it'd probably be best to use the hold all command and set(gca,'ColorOrder') as Patrick suggested. Whichever you choose for colororder is up to you, but I'd suggest going ahead and changing the default linestyleorder to something other than just '-' (the default) because each time it goes through the list of colors, it will repeat with the same line style, which is no good.
How do I undo this default set(...) command... back to "normal"?
it's not saved when you close matlab, so it will be back to normal after you restart. alternatively you can save the orders in a variable and restore them back to normal if you need to.
co = get(0,'defaultaxescolororder');
lso = get(0,'defaultaxeslinestyleorder');
set(0,'defaultaxescolororder',co);
set(0,'defaultaxeslinestyleorder',lso);
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')
Thanks for the info, Patrick, that's definitely good to know.

Sign in to comment.

More Answers (4)

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.
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

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.

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
plot(pk3x4(:,n));
end
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.
https://www.mathworks.com/matlabcentral/fileexchange/52091-plot-with-linestyles-and-markers#feedbacks

Asked:

on 21 Feb 2012

Answered:

on 20 Jun 2019

Community Treasure Hunt

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

Start Hunting!