Merging different legend labels
조회 수: 26 (최근 30일)
이전 댓글 표시
Hello everyone, I haven't spend much time coding in Matlab so this one might be a little silly question. Similar questions have been asked as far as I can see; but none of the solutions provided worked for me. So here I am asking: As can be seen in the attached code and image, I have a graph consisting of five lines, two of them representing the resulst of the first model (green ones) and the rest representing the second model (blue ones). Could not find a way to add legend label for different colours in editor. When I code and graph without legends and insert legend in the figure window, it happens to be that, as expected, there are five different labels for each line. I want to merge the green and blue ones and I am pretty sure it is possible and it is just me who cannot do it. Also in the future 8 more model results will be added, each of them consisting of one, two or three lines. How can I merge the legend labels?
Thanks in advance!
x1 = [1, 2 ,3, 4, 5];
y1 = [5, 3, 2.8, 1.7, 1.2];
y2 = [6.5, 4.7, 3.5, 1.9, 1.3];
x2 = [1, 2, 3, 4, 5];
y3 = [6, 5.6, 3.1, 2.9, 1.7];
y4 = [5, 4.1, 3.8, 2, 1.5];
y5 = [5, 4, 3, 1.8, 1.4];
plot(x1,y1,x1,y2,'color', 'g'); %Model 1
hold on
plot(x2,y3,x2,y4,x2,y5,'color', 'b'); %Model 2
hold off
grid minor
xlabel('x values')
ylabel('y values')
title('figure 1')
채택된 답변
VINAYAK LUHA
2024년 7월 8일
Hi Mahmut,
My understanding is that you have multiple line plots from different models and want to create a common legend for all line plots from a single model.
You can achieve this by using the "graphics object handles" of the plots along with the MATLAB "legend" function.
Here is a code snippet for your reference:
x1 = [1, 2 ,3, 4, 5];
y1 = [5, 3, 2.8, 1.7, 1.2];
y2 = [6.5, 4.7, 3.5, 1.9, 1.3];
x2 = [1, 2, 3, 4, 5];
y3 = [6, 5.6, 3.1, 2.9, 1.7];
y4 = [5, 4.1, 3.8, 2, 1.5];
y5 = [5, 4, 3, 1.8, 1.4];
hold on;
% Plot Model 1
h1 = plot(x1, y1, 'color', 'g');
plot(x1, y2, 'color', 'g');
% Plot Model 2
h2 = plot(x2, y3, 'color', 'b');
plot(x2, y4, 'color', 'b');
plot(x2, y5, 'color', 'b');
% Add grid, labels, and title
grid minor;
xlabel('x values');
ylabel('y values');
title('Figure 1');
% Add legend
legend([h1, h2], {'Model 1', 'Model 2'});
hold off;
For more details on the MATLAB "legend" and "Graphics Object handle", you can refer to the following documentations:
- https://www.mathworks.com/help/matlab/ref/legend.html
- https://www.mathworks.com/help/matlab/creating_plots/how-to-work-with-graphics-objects.html
Hope this answers your question
Regards,
Vinayak
댓글 수: 0
추가 답변 (2개)
Matt J
2024년 7월 8일
편집: Matt J
2024년 7월 8일
One way,
x1 = [1, 2 ,3, 4, 5];
y1 = [5, 3, 2.8, 1.7, 1.2];
y2 = [6.5, 4.7, 3.5, 1.9, 1.3];
x2 = [1, 2, 3, 4, 5];
y3 = [6, 5.6, 3.1, 2.9, 1.7];
y4 = [5, 4.1, 3.8, 2, 1.5];
y5 = [5, 4, 3, 1.8, 1.4];
Model1=plot(x1,y1,x1,y2,'color', 'g'); %Model 1
hold on
Model2=plot(x2,y3,x2,y4,x2,y5,'color', 'b'); %Model 2
hold off
grid minor
xlabel('x values')
ylabel('y values')
title('figure 1')
legend([Model1(1), Model2(1)],'Model 1','Model 2')
댓글 수: 0
Catalytic
2024년 7월 8일
x1 = [1, 2 ,3, 4, 5];
y1 = [5, 3, 2.8, 1.7, 1.2];
y2 = [6.5, 4.7, 3.5, 1.9, 1.3];
x2 = [1, 2, 3, 4, 5];
y3 = [6, 5.6, 3.1, 2.9, 1.7];
y4 = [5, 4.1, 3.8, 2, 1.5];
y5 = [5, 4, 3, 1.8, 1.4];
X1=[x1 nan x1]; Y1=[y1 nan y2];
X2=[x2 nan x2 nan x2]; Y2=[y3 nan y4 nan y5];
plot(X1,Y1,'g', X2,Y2,'b'); legend 'Model 1' 'Model 2'
grid minor
xlabel('x values')
ylabel('y values')
title('figure 1')
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Legend에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!