legend in subplot graph

3 次查看(过去 30 天)
Chaudhary P Patel
Chaudhary P Patel 2021-1-29
回答: Abhipsa 2025-5-29
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
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!

类别

Help CenterFile Exchange 中查找有关 Legend 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by