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!