how to label lines on a graph

1,444 ビュー (過去 30 日間)
Harold
Harold 2011 年 12 月 1 日
コメント済み: DGM 2024 年 11 月 27 日
I have a program which asks for variable values of k. A function inputs these into an equation and displays a line on a graph for each value of k. How can i label each of these lines with the value of k used for each. The problem i am having is that the k values are variable and so are the amount of lines displayed on the graph, so i can't figure out how to do this for the values that are fed to the function. Any help would be much appreciated? Thanks, Scott
  1 件のコメント
Zach Leo
Zach Leo 2023 年 8 月 4 日
There is a very cool package "MATLAB label line" which you can just download and add.

サインインしてコメントする。

回答 (10 件)

Maël Pouget
Maël Pouget 2021 年 8 月 10 日
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.
  1 件のコメント
Paul Lebow
Paul Lebow 2023 年 2 月 1 日
Perfect!

サインインしてコメントする。


Chad Greene
Chad Greene 2014 年 8 月 10 日
label might do the trick.
  3 件のコメント
Basil Eldeeb
Basil Eldeeb 2021 年 3 月 11 日
lol
Carolyn
Carolyn 2022 年 11 月 27 日
Bruh

サインインしてコメントする。


Michael
Michael 2011 年 12 月 1 日
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 件のコメント
David Vargas
David Vargas 2016 年 4 月 23 日
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
ananya mittal 2020 年 6 月 3 日
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
Matt Tearle 2011 年 12 月 1 日
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 件のコメント
Matt Tearle
Matt Tearle 2011 年 12 月 1 日
Great, then what I have should do the job... unless the curves all converge at the ends or something :)
Madhuri
Madhuri 2017 年 9 月 17 日
What co ordinates should I specify when I want the text to be on the first point of the curve? text (x(1),y(1), labels) is not working

サインインしてコメントする。


Igor Kubyshkin
Igor Kubyshkin 2019 年 2 月 26 日
編集済み: DGM 2024 年 1 月 16 日
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 件のコメント
Samim
Samim 2024 年 1 月 15 日
編集済み: Samim 2024 年 1 月 15 日
what to input for the variable h in the function ?
DGM
DGM 2024 年 1 月 16 日
編集済み: DGM 2024 年 1 月 16 日
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
Nathaniel W 2018 年 5 月 8 日
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)
Credit to this answer for making legend text append work.
  1 件のコメント
Gopal
Gopal 2024 年 10 月 22 日
Brilliant! Thanks !

サインインしてコメントする。


Emmanuel Adesina
Emmanuel Adesina 2021 年 4 月 24 日
@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.

Sergio Yanez-Pagans
Sergio Yanez-Pagans 2021 年 8 月 21 日

piston_pim_offset
piston_pim_offset 2023 年 11 月 14 日
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)
Error using text
Inputs must be x, y, and optionally z coordinates followed by a character vector, cell array of character vectors, string array, or categorical array.
  2 件のコメント
Voss
Voss 2023 年 11 月 14 日
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)
piston_pim_offset
piston_pim_offset 2023 年 11 月 14 日
thanks for correction

サインインしてコメントする。


Igor Kubyshkin
Igor Kubyshkin 2024 年 11 月 27 日
編集済み: Igor Kubyshkin 2024 年 11 月 27 日
Why plot command gives wrong result for complex number, when imaginary part is equal zero?
For example plot(0.5+0.*1i,'ro') depicts point in [1, 0.5] instead of [0.5, 0] .
  1 件のコメント
DGM
DGM 2024 年 11 月 27 日
That's not an answer to the question. In fact, it's not relevant to the question. This should have been asked as a new question.
I'm inclined to clean this up since it really doesn't belong here as an answer, but to answer your question:
Using the syntax plot(Y) will plot the columns of Y with respect to their row subscript, so the apparently complex scalar 0.5+0i is rendered as (1,0.5) -- because the real component is 0.5 and the row index is 1. That might already be something you are familiar with, but the problem here is that the apparently complex scalar that you created isn't actually a complex number.
p = 1 + 0*1i; % is this a complex number?
isreal(p) % no i guess not
ans = logical
1
Consider:
% these all plot fine, including the point with 0i
Z = [0.5+1*1i; 0.5+0.5*1i; 0.5+0*1i]; % this is a complex array
plot(Z,'ro','markersize',10,'linewidth',2); hold on
% this plots as if a scalar real against the index 1
p = 1 + 0*1i; % this is not a complex array
plot(p,'bx','markersize',10,'linewidth',2)
% if you're going to be plotting scalars, try to be explicit
plot(real(p),imag(p),'mo','markersize',10,'linewidth',2)
% or simply force p to be a complex scalar
plot(complex(p),'mx','markersize',10,'linewidth',2)
xlim([0 1.5])
ylim([0 1.5])
grid on

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeDiscrete Data Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by