how to add 2 legends for 2 portions of one plot?

2 次查看(过去 30 天)
For the code provided below, I have distinguished two sub-regions of one plot p1 as p1a (blue) and p1b (red). How do I give legends for p1a and p1b?
dataset1 = xlsread('C:\Users\Admin\Desktop\data1.xlsx','Sheet1');
x1=dataset1(:,1);
y1=dataset1(:,2);
p1=plot(x1,y1,'Color','blue');
fontSize=15;
title('Fibreoptic sensor output (Volts) v/s distance (mm)', 'FontSize', fontSize);
grid on;
xlabel('distace between the sensor tip and the cantilever (mm)', 'FontSize', fontSize-5);
ylabel('Sensor output (Volts)', 'FontSize', fontSize-5);
legend on;
dataset1a=xlsread('C:\Users\Admin\Desktop\data1.xlsx','Sheet1','A1:C21');
x1a=dataset1a(:,1);
y1a=dataset1a(:,2);
hold on
p1a=plot(x1a,y1a,'Color','blue')
hold on
dataset1b=xlsread('C:\Users\Admin\Desktop\data1.xlsx','Sheet1','A22:C69');
x1b=dataset1b(:,1);
y1b=dataset1b(:,2);
hold on
p1b=plot(x1b,y1b,'Color','red')
hold on

回答(2 个)

Shashank Sharma
Shashank Sharma 2019-7-17
编辑:Shashank Sharma 2019-7-17
The following plot gives rise to two legends
plot(x,y,'b');
hold on;
plot(x1,y1,'r');
legend( 'on', 'off');
matlab automatically assigns the legends to the two plots.
The following is the format for adding legends to a figure.
legend('plot 1 legend', 'plot 2 legend');

KSSV
KSSV 2019-7-17
th = 0:pi/100:2*pi ;
y = sin(th) ;
plot(th,y)
% set 1
th1 = th(th<=pi) ;
y1 = y(th<=pi) ;
% set 2
th2 = th(th>pi) ;
y2 = y(th>pi) ;
% plot
plot(th1,y1,'r')
hold on
plot(th2,y2,'b')
legend('Set1','Set2')

类别

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

产品


版本

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by