How do I set each legend on the curve in multiple plot?

조회 수: 33 (최근 30일)
Muhammad Sinan
Muhammad Sinan 2021년 2월 10일
댓글: Muhammad Sinan 2021년 2월 10일
Dear friends!
I have multiple curves plot for which I need to specify the legends automatically. I use the following script in the code;
figure(2)
txt = ['\alpha=' num2str(p)];
plot(t, L, 'displayname', txt, 'linewidth', 2 );
xlabel('Time(Days)')
ylabel('L')
lgd = legend;
lgd.NumColumns = 2
title(lgd, 'Order of FDE, \alpha')
hold on
The point is, there are 10 legends in the same plot the problem is, each legend is associated with different colors not the shape of the curves when they reached more than 4 or 5 legends it repeat the color which create confusion in recognition of the curves. Now, please help me how can I set my legends on top of each curve like the legends shown in the picture below. It is a contour plot, I need exactly as it contain. While the problem is in the curves with legends associated with beta.
Thanks in advance!

답변 (1개)

Chad Greene
Chad Greene 2021년 2월 10일
Perhaps the issue isn't the legend, but plotting the desired colors? How about specifying the colors of the plots explicitly, like this:
x = 1:100;
col = parula(10); % rgb values of 10 colors
hold on
for k = 1:10
h(k) = plot(x,k*x,'color',col(k,:),'linewidth',2)
end
legend(h,'location','northwest')
  댓글 수: 3
Chad Greene
Chad Greene 2021년 2월 10일
Yep, you'll want to loop through each curve and plot it individually. So for 10 curves,
for k = 1:1
h(k) = plot(x,Y(:,k),'color',col(k,:));
end
Or perhaps it's Y(k,:), depending on how your data are arranged. Also be sure the index of the color is the loop index k, not txt as you have written.
Muhammad Sinan
Muhammad Sinan 2021년 2월 10일
Yes, I have used the txt because it is a vector of the values of p which represent the values of \alpha. That's why I used it, I also tried the p directly but showing this error
Index in position 1 is invalid. Array indices must be positive integers or logical values.
Error in Simulation (line 44)
plot(t, S,'displayname', txt, 'linewidth', 2, 'color', col(p, :) );
>>
I don't know how? While the plot(t, S) are already correct.

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

카테고리

Help CenterFile Exchange에서 Legend에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by