legend in subplot graph
조회 수: 2 (최근 30일)
이전 댓글 표시
how to give legend for multiple graph case.
subplot(3,1,1)
plot(tn,transpose(utdelt(1,:)),'-r');
title('Displacement in X-Direction')
xlabel('time(sec)');
ylabel('Displacement (m)');
hold on
plot(tn,transpose(utdelt(28,:)),'-g');
title('Displacement in X-Direction')
xlabel('time(sec)');
ylabel('Displacement (m)');
답변 (1개)
Abhipsa
2025년 5월 29일
To give a legend for multiple plots in a subplot, you can call the “legend” function after plotting all lines in a particular subplot.
You can refer to the following code snippet, which demonstrates how to add a legend when plotting multiple graphs in a subplot:
% Example dummy data (replace with your actual data)
tn = 0:0.1:10; % Time vector
utdelt = rand(30, length(tn)); % Dummy displacement data for 30 nodes
% Subplot 1: Displacement in X-Direction
subplot(3,1,1)
plot(tn, transpose(utdelt(1,:)), '-r'); % Node 1
hold on
plot(tn, transpose(utdelt(28,:)), '-g'); % Node 28
title('Displacement in X-Direction')
xlabel('Time (sec)');
ylabel('Displacement (m)');
legend('Node 1', 'Node 28')
grid on
% Subplot 2: Displacement in Y-Direction (dummy example)
subplot(3,1,2)
plot(tn, transpose(utdelt(2,:)), '-b'); % Node 2
hold on
plot(tn, transpose(utdelt(29,:)), '-m'); % Node 29
title('Displacement in Y-Direction')
xlabel('Time (sec)');
ylabel('Displacement (m)');
legend('Node 2', 'Node 29')
grid on
% Subplot 3: Displacement in Z-Direction (dummy example)
subplot(3,1,3)
plot(tn, transpose(utdelt(3,:)), '-k'); % Node 3
hold on
plot(tn, transpose(utdelt(30,:)), '--c'); % Node 30
title('Displacement in Z-Direction')
xlabel('Time (sec)');
ylabel('Displacement (m)');
legend('Node 3', 'Node 30')
grid on
The above code will generate the following out which displays the legend for each subplot.

You can refer to the below MATLAB documentation of “legend” for more details:
I hope this helps!
댓글 수: 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!