Label more than 7 lines in a plot

조회 수: 47 (최근 30일)
MiauMiau
MiauMiau 2016년 9월 29일
댓글: Voss 2024년 2월 22일
Hi,
I am realizing that after 7 lines, Matlab seems to run out of colors for graphs and legends..is that correct? Currently I am plotting the following:
plot(matA);
legend('1.','2.','3.','4.','5.','6.','7.','8.');
xlabel('Timepoints');
ylabel('Sources');
matA is a 200x8 matrix, but after plotting I see that the lines 1 and 8 both have the same colors..what is the easiest way out?
Thanks
  댓글 수: 2
Praise
Praise 2024년 2월 22일
이동: Voss 2024년 2월 22일
how do i crwate eate a plot with 7 lines
Voss
Voss 2024년 2월 22일
@Praise: Here's one way:
plot(rand(10,7))
legend()

댓글을 달려면 로그인하십시오.

답변 (2개)

Renato Agurto
Renato Agurto 2016년 9월 29일
Hi,
one solution is to plot in different lines styles:
hold on;
plot(matA(:,1:7), '-');
plot(matA(:,8:14), '--');
...
In your example
hold on;
plot(matA(:,1:7), '-');
plot(matA(:,8), '--');
or maybe:
hold on;
plot(matA(:,1:4), '-');
plot(matA(:,5:8), '--');

Adam
Adam 2016년 9월 29일
편집: Adam 2016년 9월 29일
You can set your own colours for lines you plot and you can also specify the colours used on an axes by setting the
ColorOrder
property of an axes. By default this is a 7x3 array. You can extend it or replace it with any number of rows you want so long as it is nx3.
The legend doesn't run out of colours either, it just reflects the colours assigned to the plots so if they repeat then obviously so does it otherwise it would not be reflecting the curves correctly.
  댓글 수: 2
MiauMiau
MiauMiau 2016년 9월 29일
I didn't know that, many thanks! Now I almost got it, with including these lines:
newColors = [0 0 1; 0 0.5 0; 1 0 0; 0 0.75 0.75; 0.75 0 0.75; 0.75 0.75 0;
0.25 0.25 0.25; 0.8500 0.3250 0.0980];
co = get(gca,'ColorOrder') % Initial
% Change to new colors.
set(gca, 'ColorOrder', newColors, 'NextPlot', 'replacechildren');
co = get(gca,'ColorOrder') % Verify it changed
However now the new colors are only applied to the following plot (and not the later ones). How can I change it for all? And more importantly, where can I look up for more info on "Colororder"? Help did not give me any results, and the Matlab Colororder page does not seem to contain much information neither..
Adam
Adam 2016년 9월 29일
It looks as though the plot function over-rides these so you have to set the default axes color order before plotting instead - e.g.
set(groot,'defaultAxesColorOrder', newColors)
doSomePlotting( )
If you want to return to the defaults after you can type:
set(groot,'defaultAxesColorOrder', 'default' )

댓글을 달려면 로그인하십시오.

카테고리

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