フィルターのクリア

Merging different legend labels

67 ビュー (過去 30 日間)
Mahmut Cenk
Mahmut Cenk 2024 年 7 月 8 日 10:05
コメント済み: Mahmut Cenk 2024 年 7 月 8 日 12:59
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')
  1 件のコメント
Mahmut Cenk
Mahmut Cenk 2024 年 7 月 8 日 12:59
Endless thanks to everyone answered. All three suggestions work really well. Thank you guys.

サインインしてコメントする。

採用された回答

VINAYAK LUHA
VINAYAK LUHA 2024 年 7 月 8 日 11:08
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:
Hope this answers your question
Regards,
Vinayak

その他の回答 (2 件)

Matt J
Matt J 2024 年 7 月 8 日 10:58
編集済み: Matt J 2024 年 7 月 8 日 10:58
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')

Catalytic
Catalytic 2024 年 7 月 8 日 11:04
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')

カテゴリ

Help Center および File ExchangeLegend についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by