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

조회 수: 33 (최근 30일)
Doron
Doron 2012년 2월 21일
답변: Bruno Melo 2019년 6월 20일
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에서 Annotations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by