change linestyleorder like colororder (without a loop)

21 Ansichten (letzte 30 Tage)
Bernd
Bernd am 1 Mär. 2019
Beantwortet: Leone Campos am 12 Jul. 2022
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 Kommentar
Adam
Adam am 1 Mär. 2019
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.

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Steven Lord
Steven Lord am 1 Mär. 2019
"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."

Leone Campos
Leone Campos am 12 Jul. 2022
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

Kategorien

Mehr zu Line Plots finden Sie in Help Center und File Exchange

Produkte


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by