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

14 次查看(过去 30 天)
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 个评论
Ali
Ali 2017-10-29
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>>

请先登录,再进行评论。

采纳的回答

Kevin Holst
Kevin Holst 2012-2-21
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 个评论
Patrick Kalita
Patrick Kalita 2012-2-22
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 2012-2-23
Thanks for the info, Patrick, that's definitely good to know.

请先登录,再进行评论。

更多回答(4 个)

Jarrod Rivituso
Jarrod Rivituso 2012-2-21
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 2012-2-21
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 个评论
Hems
Hems 2018-2-8
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?

请先登录,再进行评论。


Edward Byers
Edward Byers 2013-5-23
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

Bruno Melo
Bruno Melo 2019-6-20
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

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by