how to label lines on a graph

조회 수: 2,697(최근 30일)
Harold 2011년 12월 1일
답변: Sergio Yanez-Pagans 2021년 8월 21일
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


Chad Greene
Chad Greene 2014년 8월 10일
label might do the trick.
  댓글 수: 2
Basil Eldeeb
Basil Eldeeb 2021년 3월 11일

댓글을 달려면 로그인하십시오.

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:
Returns "object1" if a=1, "object2" if a=2, etc.,
Hope this helps, though not a complete answer. Mike
  댓글 수: 3
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
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
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

댓글을 달려면 로그인하십시오.

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();
for i = 1:10
plot(i*(1:10),'DisplayName',['Curve #' num2str(i)])
does the trick (for anyone who, like me, tumbles on this question while trying to name their lines within the plot() call.

Igor Kubyshkin
Igor Kubyshkin 2019년 2월 26일
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)
i = round(ksi*numel(x));
% Get the local slope
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);
  댓글 수: 5
nlm 2020년 7월 30일
@ Holden Tranquillo
How did you get this to work ?

댓글을 달려면 로그인하십시오.

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:
plots = plot(t,x,t,2*x);
hold on
if val
Credit to this answer for making legend text append work.

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))
if max(dtm) < 0
text(max(x), min(y), num2str(k))
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일

Community Treasure Hunt

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

Start Hunting!

Translated by