# how to label lines on a graph

1,554 views (last 30 days)

Show older comments

##### 1 Comment

Zach Leo
on 4 Aug 2023

There is a very cool package "MATLAB label line" which you can just download and add.

### Answers (9)

Maël Pouget
on 10 Aug 2021

I know the question is 10 years old, but

plot(curve,'DisplayName','Curve name for the legend')

% or

ax = axes();

hold(ax,'on');

for i = 1:10

plot(i*(1:10),'DisplayName',['Curve #' num2str(i)])

end

hold(ax,'off');

legend();

does the trick (for anyone who, like me, tumbles on this question while trying to name their lines within the plot() call.

Michael
on 1 Dec 2011

The general method would be using the legend function, to which you give a vector of the plot object handles and a series of corresponding strings. For example,

figure; hold on

a1 = plot(x,y1); M1 = "Curve 1";

a2 = plot(x,y2); M2 = "Curve 2";

legend([a1,a2], [M1, M2]);

The legend makes the connection between the plot object a1 and the string M1, and uses this to generate the legend. All you must do in your loop is devise a way to generate ai and Mi for a general integer i. I don't have MATLAB open now to test it but I think there must be a way using a combination of sprintf/num2str:

sprintf('object%s',num2str(a));

Returns "object1" if a=1, "object2" if a=2, etc.,

Hope this helps, though not a complete answer. Mike

##### 3 Comments

David Vargas
on 23 Apr 2016

This also will not work. The legend uses a vector of plots and a list of labels, and the double quotes should be single quotes:

figure; hold on

a1 = plot(x,y1); M1 = 'Curve 1';

a2 = plot(x,y2); M2 = 'Curve 2';

legend([a1;a2], M1, M2)

ananya mittal
on 3 Jun 2020

Is there any way of naming around 100 curves in a plot ? This manual way can be very time taking.

Thanks in advance.

Matt Tearle
on 1 Dec 2011

How about overlaying text on the graph, next to the lines?

line(x,y) % or plot(x,y) with a hold on

text(max(x),max(y),num2str(k))

You could play with the (x,y) location of the text. When you say "line" do you mean a straight line or, in general, a curve? The former would make calculating placement very easy; the latter would require some more cleverness, unless max or min works for you.

##### 3 Comments

Matt Tearle
on 1 Dec 2011

Madhuri
on 17 Sep 2017

Igor Kubyshkin
on 26 Feb 2019

Edited: DGM
on 16 Jan 2024

function text2line(h,ksi,z,T)

% Inserts text T in/near line with handle h

% ksi - relative distance from the beginning of curve,

% z - shift along normal to curve

%

set(gcf, 'CurrentObject', h)

x=h.XData;

y=h.YData;

i = round(ksi*numel(x));

% Get the local slope

dy=y(i+1)-y(i-1);

dx=x(i+1)-x(i-1);

d = dy/dx;

X = diff(get(gca, 'xlim'));

Y = diff(get(gca, 'ylim'));

p = pbaspect;

a = atan(d*p(2)*X/p(1)/Y)*180/pi;

% Display the text

switch z==0

case 1

text(x(i), y(i), T,'HorizontalAlignment','center', 'BackgroundColor', 'w', 'rotation', a);

case 0

ez=[dy,-dx]/norm([dy,-dx]); % unit normal vector

text(x(i)+z*ez(1), y(i)+z*ez(2), T, 'HorizontalAlignment','center', 'rotation', a);

end

##### 7 Comments

DGM
on 16 Jan 2024

Edited: DGM
on 16 Jan 2024

Here's an example. Bear in mind that the offset parameter doesn't really work right unless dataaspect is unitiy.

% fake data

x = linspace(-5,5);

y = x.^3-12*x;

% plot the line

hp = plot(x,y); % get the handle for the line object

% add the label

str = 'banana'; % label text

ksi = 0.35; % normalized position along the curve

os = 0; % offset distance from the curve (doesn't work)

text2line(hp,ksi,os,str);

function text2line(h,ksi,z,T)

% Inserts text T in/near line with handle h

% ksi - relative distance from the beginning of curve,

% z - shift along normal to curve

%

set(gcf, 'CurrentObject', h)

x=h.XData;

y=h.YData;

i = round(ksi*numel(x));

i = min(max(i,2),numel(x)-1);

% Get the local slope

dy=y(i+1)-y(i-1);

dx=x(i+1)-x(i-1);

d = dy/dx;

X = diff(get(gca, 'xlim'));

Y = diff(get(gca, 'ylim'));

p = pbaspect;

a = atan(d*p(2)*X/p(1)/Y)*180/pi;

% Display the text

switch z==0

case 1

text(x(i), y(i), T,'HorizontalAlignment','center', 'BackgroundColor', 'w', 'rotation', a);

case 0

ez=[dy,-dx]/norm([dy,-dx]); % unit normal vector

text(x(i)+z*ez(1), y(i)+z*ez(2), T, 'HorizontalAlignment','center', 'rotation', a);

end

end

Nathaniel W
on 8 May 2018

Really late answer, but I was trying to do this same thing earlier. Here's a solution that works, and allows you to use if statments to add additional plots and labels:

t=1:1:10;

x=t;

val=true;

plots = plot(t,x,t,2*x);

names={'x=t','x=2t'};

hold on

if val

plots(end+1)=plot(t,x.*x);

names{end+1}='x=t^2';

end

legend(plots,names)

##### 0 Comments

Emmanuel Adesina
on 24 Apr 2021

@Matt Tearle Solution works fine, but puts all labels almost on same line...

line(x,y) % or plot(x,y) with a hold on

if max(y) > 0

text(max(x), max(y), num2str(k))

end

if max(dtm) < 0

text(max(x), min(y), num2str(k))

end

This checks if the plot is positive or negative and places the label accordingly

If you set both as min, it will place it at the origin of the line.

I hope there's a means to set it at the mid-point or other coordinates on the line.

##### 0 Comments

piston_pim_offset
on 14 Nov 2023

x = linspace(-5,5);

y = x.^3-12*x;

plot(x,y)

xt = [-2 2]; % Location the text will be

str = 'dy/dx = 0'; % text

text(xt,str)

##### 2 Comments

Voss
on 14 Nov 2023

x = linspace(-5,5);

y = x.^3-12*x;

plot(x,y)

xt = [-2 2]; % Location the text will be

str = 'dy/dx = 0'; % text

text(xt(1),xt(2),str)

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!