How Do You Label Plots in MATLAB Step-by-Step?
Creating clear and informative plots is a fundamental skill for anyone working with data visualization in MATLAB. Whether you’re analyzing scientific data, presenting engineering results, or simply exploring trends, properly labeling your plots ensures that your audience can easily understand the story your data tells. Knowing how to label plots in MATLAB not only enhances the readability of your graphs but also adds a professional touch to your work.
Plot labeling in MATLAB encompasses a variety of elements—from axis titles and legends to annotations and data point labels. Mastering these tools allows you to highlight key insights, differentiate between multiple data sets, and provide context that transforms raw numbers into meaningful information. As you delve deeper, you’ll discover how MATLAB’s versatile labeling functions can be tailored to suit simple line graphs or complex multidimensional plots alike.
Understanding the principles behind effective plot labeling is essential for clear communication in any technical field. This article will guide you through the essentials of labeling in MATLAB, setting the stage for you to create visually compelling and informative graphics. Prepare to enhance your data presentations with labels that not only inform but also engage your audience.
Customizing Axis Labels and Titles
In MATLAB, enhancing the clarity and presentation of plots often involves customizing axis labels and titles. This customization is crucial for making your plots more informative and visually appealing. The functions `xlabel`, `ylabel`, and `title` are commonly used to add descriptive text to the x-axis, y-axis, and the plot itself, respectively.
You can modify the font size, font weight, color, and style of these labels by using name-value pair arguments or by accessing the properties of the label objects. For example, setting the font size and color can be done as follows:
matlab
xlabel(‘Time (s)’, ‘FontSize’, 12, ‘FontWeight’, ‘bold’, ‘Color’, ‘r’);
ylabel(‘Amplitude’, ‘FontSize’, 12, ‘FontWeight’, ‘bold’, ‘Color’, ‘b’);
title(‘Signal over Time’, ‘FontSize’, 14, ‘FontWeight’, ‘bold’);
If more detailed customization is needed, you can capture the handle of the label or title and modify its properties individually:
matlab
hx = xlabel(‘Frequency (Hz)’);
hx.FontSize = 14;
hx.FontAngle = ‘italic’;
hx.Color = [0 0.5 0];
This approach provides finer control over label appearance.
Adding Legends and Their Customization
Legends are essential in multi-line or multi-data plots to distinguish between different data series. The `legend` function in MATLAB allows you to create and customize legends easily.
Basic usage involves passing the label strings corresponding to each plotted data series:
matlab
plot(x, y1, x, y2);
legend(‘Data Set 1’, ‘Data Set 2’);
The legend function also supports numerous customization options, such as location, font size, orientation, and box visibility. Some common properties include:
- `’Location’`: Controls where the legend appears (e.g., `’northwest’`, `’southoutside’`).
- `’FontSize’`: Sets the font size of legend text.
- `’Orientation’`: Can be `’vertical’` or `’horizontal’`.
- `’Box’`: Turns the border box on or off (`’on’`/`’off’`).
Example:
matlab
legend(‘Experiment A’, ‘Experiment B’, ‘Location’, ‘bestoutside’, ‘FontSize’, 10, ‘Box’, ‘off’);
For more complex plots, you can obtain the legend handle and modify properties programmatically:
matlab
lgd = legend(‘Data 1’, ‘Data 2’);
lgd.TextColor = ‘blue’;
lgd.FontWeight = ‘bold’;
Labeling Specific Data Points
Sometimes, it is important to label individual points on a plot to highlight particular values or annotations. MATLAB offers several ways to do this, such as using the `text` function.
The `text` function places text at specified coordinates on the plot:
matlab
plot(x, y, ‘o’);
text(x(5), y(5), ‘Peak’, ‘FontSize’, 12, ‘Color’, ‘red’);
This places the label “Peak” at the fifth data point. You can adjust the position by adding offsets to avoid overlap:
matlab
text(x(5) + 0.1, y(5), ‘Peak’, ‘FontSize’, 12, ‘Color’, ‘red’);
MATLAB also provides the `datacursormode` interactive tool, allowing users to click on data points and display labels dynamically.
Using LaTeX and Special Characters in Labels
To include mathematical expressions and special characters in your plot labels, MATLAB supports LaTeX markup within label strings. Enclose the LaTeX code in dollar signs `$…$` or use the `’Interpreter’` property set to `’latex’`.
Example:
matlab
xlabel(‘Time (s)’, ‘Interpreter’, ‘latex’);
ylabel(‘$\sin(\omega t)$’, ‘Interpreter’, ‘latex’);
title(‘Plot of $\sin(\omega t)$ vs. Time’, ‘Interpreter’, ‘latex’);
Special characters can also be used with the `’tex’` interpreter, which is the default. Common symbols include Greek letters and superscripts/subscripts:
- Greek letters: `\alpha`, `\beta`, `\gamma`
- Superscripts: `x^{2}`
- Subscripts: `x_{i}`
| Symbol | LaTeX Code | Appearance |
|---|---|---|
| Alpha | `\alpha` | α |
| Beta | `\beta` | β |
| Gamma | `\gamma` | γ |
| Superscript 2 | `x^{2}` | x² |
| Subscript i | `x_{i}` | xᵢ |
| Integral sign | `\int` | ∫ |
Adjusting Label Positions and Orientation
At times, default label positions may overlap with plot elements or not suit the desired layout. MATLAB allows manual adjustment of label positions and text orientation.
For axis labels, you can modify their position by accessing their `Position` property:
matlab
hx = xlabel(‘X Axis’);
pos = hx.Position; % pos is a 3-element vector [x y z]
hx.Position = pos + [0 0.1 0]; % Move label slightly upward
Text labels created with `text` also support rotation via the `’Rotation’` property:
matlab
text(x(3), y(3), ‘Sample Point’, ‘Rotation’, 45);
This rotates the text 45 degrees counterclockwise.
Summary of Common Labeling Functions and Properties
| Function/Property | Description | Example Usage |
|---|
| Function | Syntax Example | Description |
|---|---|---|
title |
title('Plot Title') |
Sets the title for the current axes. |
xlabel |
xlabel('Time (s)') |
Labels the x-axis with the specified string. |
ylabel |
ylabel('Amplitude') |
Labels the y-axis with the specified string. |
zlabel |
zlabel('Depth (m)') |
Labels the z-axis for 3D plots. |
Example usage in MATLAB code:
x = 0:0.1:10;
y = sin(x);
plot(x, y);
title('Sine Wave');
xlabel('Time (s)');
ylabel('Amplitude');
Using Legends to Identify Data Series
Legends are essential when multiple data series are plotted together. The legend function identifies each plotted line or marker with descriptive labels.
Basic syntax:
legend('Series 1', 'Series 2', ..., 'Location', 'best')
Common locations include 'best', 'northwest', 'southeast', etc.
Example:
plot(x, sin(x), '-b', x, cos(x), '--r');
legend('sin(x)', 'cos(x)', 'Location', 'best');
Additional legend customization options include:
'FontSize'to adjust text size.'TextColor'to specify label colors.'Box'to toggle the legend box.
Adding Text Annotations Within Plots
The text function allows the insertion of text at arbitrary positions within the plot area, defined by data coordinates.
Syntax:
text(x, y, 'Label')
Example:
plot(x, y);
text(5, 0, 'Center Point', 'FontSize', 12, 'Color', 'red');
This inserts the label “Center Point” at the coordinate (5,0) on the plot. The text function accepts additional parameters such as font size, color, font weight, and rotation.
Using Annotation for Enhanced Labeling
MATLAB’s annotation function offers more complex graphical labeling options such as arrows, text boxes, and shapes, which can be positioned relative to the figure window or axes.
Basic usage example:
annotation('textbox', [x y w h], 'String', 'Annotation Text', 'FontSize', 10, 'BackgroundColor', 'white');
annotation('arrow', [x_start x_end], [y_start y_end]);
Coordinates for annotations are normalized relative to the figure window (values between 0 and 1). This allows annotations to remain in fixed positions regardless of axis zooming or resizing.
Customizing Label Appearance
Most labeling functions support property name-value pairs to customize appearance. Common properties include:
| Property | Description | Expert Insights on How To Label Plots In Matlab
|---|

