user3535716
user3535716

Reputation: 255

How to plot the name of a line segment in Matlab?

I want to add the name to the line segment plotted in the figure. For example, I plot the segment containing two endpoints (0, 0) and (3, 3), and its name is segment1. How to draw its name “segment1" at the position (0, 1)?

Thanks

Upvotes: 0

Views: 617

Answers (2)

Benoit_11
Benoit_11

Reputation: 13945

You want to use a text annotation using this syntax:

  figure

plot([0 3], [0 3]);
hText = text(1 ,0.7,'Segment 1','FontSize',16) % Using (1,1) as a position was not looking good :)

enter image description here

Upvotes: 2

Dennis Jaheruddin
Dennis Jaheruddin

Reputation: 21563

I think you are looking for a way to add text to a graph.

This example should be a good place to start, it shows you how to use the text command or the legend:

enter image description here

% Define initial conditions
t0 = 0;
tfinal = 15;
y0 = [20 20]';
% Simulate the differential equation
tfinal = tfinal*(1+eps);
[t,y] = ode23('lotka',[t0 tfinal],y0);
% Plot the two curves, storing handles to them
% so their DisplayNames can be set
hlines = plot(t,y);
% Compose and display two multiline text
% annotations as cell arrays
str1(1) = {'Many Predators;'};
str1(2) = {'Prey Population'};
str1(3) = {'Will Decline'};
text(7,220,str1)
str2(1) = {'Few Predators;'};
str2(2) = {'Prey Population'};
str2(3) = {'Will Increase'};
text(5.5,125,str2)
% Set DisplayNames for the lines for use by the legend
set(hlines(1),'Displayname','Prey')
set(hlines(2),'Displayname','Predator')
% Center a legend at the top of the graph
legend('Location','north')
% Add a title with bold style
title('Lotka-Volterra Predator-Prey Population Model',... 
  'FontWeight','bold')

Note that the link also describes more advanced features.

Upvotes: 2

Related Questions