How to assign a different color to lines being plotted in a loop?
조회 수: 86 (최근 30일)
이전 댓글 표시
Hi All
I am plotting n different lines in one single plot. but what I need it to assign a different color ( even random) for each iteration. and then use it as a Legend.
How do I do this ?
댓글 수: 1
Stephen23
2020년 5월 12일
The accepted answer states that line color is determined by the lines colormap. This is not correct.
Line colors are determined by the axes ColorOrder property:
The lines colormap is also defined in terms of the axes ColorOrder.
채택된 답변
Akihumi
2020년 5월 8일
편집: Akihumi
2020년 5월 12일
* Edits: Sorry for the wrong information about the default line color.
Otherwise, you can try
figure
hold on
randColor = rand(10,3);
for i = 1:10
plot(x(:,i),y(:,i),'Color',randColor(i,:));
end
legend
But the color would be ugly. Instead of rand, you can try other available colormap. For example
figure
hold on
cm = parula(10);
for i = 1:10
plot(x(:,i),y(:,i),'Color', cm(i,:));
end
legend
댓글 수: 1
Ameer Hamza
2020년 5월 8일
A correction: By default, the color of lines are chosen from ColorOrder property of the axes, not colormap: https://www.mathworks.com/help/releases/R2020a/matlab/ref/matlab.graphics.axis.axes-properties.html#budumk7_sep_shared-ColorOrder
추가 답변 (1개)
Isiah Pham
2020년 5월 8일
편집: Isiah Pham
2020년 5월 12일
You can make a cell array for each color you want and the pull from it
colors = {['red'],['blue']}
for rep = 1:2
plot((x,rep.*x), colors{rep})
end
This will plot two lines, the first red and the second blue
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Distribution Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!