change linestyleorder like colororder (without a loop)
조회 수: 22 (최근 30일)
이전 댓글 표시
To use the plot function without a for loop, the code below gives a solution to change the color for every new line:
figure
hold on;
Data_to_plot = [repmat(1,1,5); repmat(5,1,5); repmat(10,1,5)];
Color_type = [ 0, 0, 1; 0,1,0; 1,0,0];
set(gca, 'ColorOrder', Color_type, 'NextPlot', 'replacechildren');
plot([1:1:5], Data_to_plot)
But if I want to do the same for the linestyle, it doesn't work:
figure
hold on;
Data_to_plot = [repmat(1,1,5); repmat(5,1,5); repmat(10,1,5)];
Line_type = {'--', '-', ':'};
set(gca, 'LineStyleOrder', Line_type, 'NextPlot', 'replacechildren');
plot([1:1:5], Data_to_plot)
How can i solve this problem to change Color AND linestyle for several lines in a plot without using a foor loop
댓글 수: 1
Adam
2019년 3월 1일
From playing around with this it seems that the plots cycle round the colours for each line, but only cycle round the line styles once for all colours, so if you have 7 colours set in ColorOrder then you get 7 lines of different colours with the same linie style, then the 8th will loop round back to the first colour and take on the 2nd line style.
I can see nothing in the documentation (after a quick scan anyway) that suggests this is how it should behave. My understanding from the help is that ColorOrderIndex and LineStyleOrderIndex should independently increment, each with the wrap around logic applied rather than one effectively being an outer loop and the other an inner loop.
답변 (2개)
Steven Lord
2019년 3월 1일
"Axes increments the line style only after using all of the colors in the ColorOrder property. It then uses all the colors again with the second line style, and so on."
댓글 수: 0
Leone Campos
2022년 7월 12일
Sorry, I've used a for loop, but it is better then nothing :)
MathWorks has changed this index behavior since MATALB R2019b. As I'm using R2018a, I was not able to solve this without a for loop.
If someone else wants to control both Color and LineStyle and doesn't mind using a for loop, I've got the result by controlling LineStyleOrderIndex and ColorOrderIndex manually.
(Of course, the downside of this approach is that you can't plot everything at once, but must use a for loop instead)
Data_to_plot = [repmat(1,1,5); repmat(5,1,5); repmat(10,1,5)];
Line_type = {'--', '-', ':'};
Color_type = [0,0,1; 0,1,0; 1,0,0];
figure
ax = gca;
set(ax, 'ColorOrder', Color_type, 'LineStyleOrder', Line_type, 'NextPlot', 'replacechildren')
for i=1:size(Data_to_plot,1)
plot([1:1:5], Data_to_plot(i,:));
ax.LineStyleOrderIndex = ax.ColorOrderIndex; % force LineStyleOrderIndex to update together with ColorOrderIndex
hold on
end
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Line Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!