MATLAB Answers

0

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

Asked by AISWARYA LAKSHMI on 17 Jul 2019
Latest activity Answered by KSSV
on 17 Jul 2019
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

  0 Comments

Sign in to comment.

Products


Release

R2017b

2 Answers

Answer by Shashank Sharma on 17 Jul 2019
Edited by KSSV
on 17 Jul 2019

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');

  0 Comments

Sign in to comment.


Answer by KSSV
on 17 Jul 2019

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')

  0 Comments

Sign in to comment.