0
0
MatlabHow-ToBeginner ยท 3 min read

How to Annotate Plot in MATLAB: Simple Guide with Examples

To annotate a plot in MATLAB, use functions like title to add a title, xlabel and ylabel to label axes, legend to describe plotted data, and text to place custom text at specific points on the plot. These functions help explain and highlight important parts of your graph.
๐Ÿ“

Syntax

Here are the basic MATLAB functions to annotate plots:

  • title('Your Title'): Adds a title to the plot.
  • xlabel('X-axis Label'): Labels the x-axis.
  • ylabel('Y-axis Label'): Labels the y-axis.
  • legend('Label1', 'Label2'): Adds a legend describing plotted data.
  • text(x, y, 'Your Text'): Places custom text at coordinates (x, y) on the plot.

Each function takes strings or coordinates to specify the annotation content and position.

matlab
title('Plot Title')
xlabel('X-axis Label')
ylabel('Y-axis Label')
legend('Data 1', 'Data 2')
text(2, 3, 'Sample Text')
๐Ÿ’ป

Example

This example shows how to plot two lines and add annotations like title, axis labels, legend, and custom text on the plot.

matlab
x = 1:5;
y1 = x;
y2 = x.^2;
plot(x, y1, '-o', x, y2, '-s')
title('Example Plot')
xlabel('X Values')
ylabel('Y Values')
legend('Linear', 'Quadratic')
text(3, 10, 'Point (3,10)')
Output
A plot window opens showing two lines: one linear and one quadratic, with a title 'Example Plot', x-axis labeled 'X Values', y-axis labeled 'Y Values', a legend identifying the lines, and the text 'Point (3,10)' near the coordinate (3,10).
โš ๏ธ

Common Pitfalls

Common mistakes when annotating plots in MATLAB include:

  • Using text coordinates outside the plot range, so the text does not appear.
  • Forgetting to call legend after plotting multiple lines, which makes it unclear what each line represents.
  • Not using string quotes properly, causing syntax errors.
  • Overlapping text or labels that make the plot hard to read.

Always check your axis limits and adjust text positions if needed.

matlab
plot(1:5, (1:5).^2)
text(10, 100, 'Too Far') % Text outside plot area
legend('Quadratic') % Correct usage

% Wrong: legend without quotes
% legend(Quadratic) % This causes error

% Right: legend with quotes
legend('Quadratic')
Output
A plot of quadratic data with a legend 'Quadratic'. The text 'Too Far' does not appear because coordinates (10,100) are outside the plot range.
๐Ÿ“Š

Quick Reference

FunctionPurposeExample Usage
titleAdd a title to the plottitle('My Plot')
xlabelLabel the x-axisxlabel('Time (s)')
ylabelLabel the y-axisylabel('Speed (m/s)')
legendAdd legend for plotted datalegend('Data1', 'Data2')
textPlace text at (x,y) coordinatestext(2, 5, 'Note')
โœ…

Key Takeaways

Use title, xlabel, ylabel, legend, and text functions to annotate MATLAB plots clearly.
Ensure text coordinates are within the plot limits to make annotations visible.
Always use quotes around strings in annotation functions to avoid syntax errors.
Place legends after plotting all data to correctly describe each line or marker.
Adjust text positions to prevent overlapping and keep the plot readable.