MATLAB - Quick Tips - How to plot multiple legends

TB Mechanical Engineering
TB Mechanical Engineering
2.7 هزار بار بازدید - 2 سال پیش - %% Matlab code to show
%% Matlab code to show method how to plot multiple legends in a single figure
% Written for Youtube channel TB Mechanical Engineering
% January 2022
% Thanks for watching, Tomas

clear all
close all
clc

%% Create Figure and Axes
set(0,'Units','centimeters')
FigW=21;
FigH=0.5*FigW;

set(0,'DefaultAxesFontName', 'Times New Roman')
set(0,'DefaultAxesFontSize', 15)
set(0, 'DefaultLineLineWidth', 2);
set(0, 'DefaultAxesLineWidth', 1);

FigHandle =  figure('Name','Plot','color',[1 1 1],...
           'PaperUnits','centimeters','PaperSize',[FigW FigH],...
           'PaperPosition',[0,0,FigW,FigH],'Units','centimeters',...
           'Position',[3,3,FigW,FigH]);
Ax1=axes;
hold on

%% Dataset A - harmonic signal
t = 0 : 0.001 : 5; % time
F = [2 5 10]; % frequency in Hz
Amp = 4; % Amplitude in mm

SigA1 = Amp*sin(2*pi*F(1)*t);
SigA2 = Amp*sin(2*pi*F(2)*t);
SigA3 = Amp*sin(2*pi*F(3)*t);

%% Plot A    
HanPlotA(1) = plot(Ax1,t,SigA1,'Color','b','LineStyle','-');
HanPlotA(2) = plot(Ax1,t,SigA2,'Color','b','LineStyle','-.');
HanPlotA(3) = plot(Ax1,t,SigA3,'Color','b','LineStyle',':');

%% Dataset B - linear function
C_lin = [5 7.5 10]; % linear coefficient
SigB1 = C_lin(1)*t;
SigB2 = C_lin(2)*t;
SigB3 = C_lin(3)*t;

%% Plot B    
HanPlotB(1) = plot(Ax1,t,SigB1,'Color','r','LineStyle','-');
HanPlotB(2) = plot(Ax1,t,SigB2,'Color','r','LineStyle','-.');
HanPlotB(3) = plot(Ax1,t,SigB3,'Color','r','LineStyle',':');

%% Dataset C - exponential function
C_exp = [0.5 1 2]; % exponential coefficient
SigC1 = exp(C_exp(1)*t);
SigC2 = exp(C_exp(2)*t);
SigC3 = exp(C_exp(3)*t);

%% Plot C    
HanPlotC(1) = plot(Ax1,t,SigC1,'Color','g','LineStyle','-');
HanPlotC(2) = plot(Ax1,t,SigC2,'Color','g','LineStyle','-.');
HanPlotC(3) = plot(Ax1,t,SigC3,'Color','g','LineStyle',':');
xlim([0 0.5])

%% Title and Axis Labels
title('Multiple Legends')
ylabel('Displacement (mm)')
xlabel('Time (s)')

%% 1st legend
legend(Ax1,HanPlotA(:),{['f_{har} = ' num2str(F(1)) ' Hz'];...
   ['f_{har} = ' num2str(F(2)) ' Hz']; ['f_{har} = ' num2str(F(3)) ' Hz']},...
   'Location','NorthEast');

%% 2nd legend
% invisible dummy axes object for the second legend
Ax2=axes('Position',get(gca,'Position'),'Visible','Off');

legend(Ax2,HanPlotB(:),{['C_{lin} = ' num2str(C_lin(1))];...
   ['C_{lin} = ' num2str(C_lin(2))]; ['C_{lin} = ' num2str(C_lin(3))]},...
   'Location','NorthWest');

%% 3rd legend
% invisible dummy axes object for the third legend
Ax3=axes('Position',get(gca,'Position'),'Visible','Off');

legend(Ax3,HanPlotC(:),{['C_{exp} = ' num2str(C_exp(1))];...
   ['C_{exp} = ' num2str(C_exp(2))]; ['C_{exp} = ' num2str(C_exp(3))]},...
   'Location','SouthWest');

%% 4th legend
% invisible dummy axes object for the fourth legend
Ax4=axes('Position',get(gca,'Position'),'Visible','Off');
hold on
% Dummy plots, otherwise the legend values would be rewritten
HanPlotDummy(1) = plot(Ax4,0,0,'Color','b','LineStyle','-');
HanPlotDummy(2) = plot(Ax4,0,0,'Color','r','LineStyle','-');
HanPlotDummy(3) = plot(Ax4,0,0,'Color','g','LineStyle','-');

legend(Ax4,HanPlotDummy(:),'Harmonic','Linear','Exponential',...
   'Location','SouthEast');
2 سال پیش در تاریخ 1400/11/24 منتشر شده است.
2,700 بـار بازدید شده
... بیشتر