To annotate MATLAB graphs with text arrows (text annotations connected to an arrow), you can use the annotation function or the textarrow function.
To annotate MATLAB graphs with text arrows (text annotations connected to an arrow), you can use the annotation
function or the textarrow
function. Here’s a step-by-step guide:
1. Basic Text Arrow Using annotation
The annotation
function allows you to add arrows with text labels.
Syntax:
annotation('textarrow', [x_start, x_end], [y_start, y_end], 'String', 'Your Text');
Example:
% Create a plot x = 0:0.1:2*pi; y = sin(x); plot(x, y); hold on; % Add a text arrow pointing to a peak annotation('textarrow', [0.5, 0.6], [0.7, 0.8], ... 'String', 'Peak Here', ... 'FontSize', 10, ... 'Color', 'red', ... 'TextColor', 'black');
[x_start, x_end]
and[y_start, y_end]
are normalized coordinates (relative to the figure window, from 0 to 1).- Adjust positions by trial and error or use
ginput
to interactively select coordinates.
2. Using textarrow
(Object-Oriented Approach)
For more control over arrow properties, use textarrow
:
% Create a text arrow in data coordinates (not normalized) ta = textarrow([x_start, x_end], [y_start, y_end], 'Your Text'); set(ta, 'Color', 'blue', 'LineWidth', 1.5, 'HeadLength', 10);
Example:
plot(x, y); hold on; ta = textarrow([pi/2, pi/2], [1, 0.8], 'Max Value'); set(ta, 'FontSize', 12, 'HorizontalAlignment', 'center');
3. Positioning in Data Coordinates
To place the arrow using data coordinates (e.g., matching the plot’s x/y axis):
- Convert data coordinates to normalized figure coordinates using
dsxy2figxy
(custom function below). - Use the normalized coordinates in
annotation
.
Custom Helper Function:
function [x_norm, y_norm] = dsxy2figxy(ax, x_data, y_data) % Convert data coordinates to normalized figure coordinates pos = ax.Position; x_norm = (x_data - ax.XLim(1)) / diff(ax.XLim) * pos(3) + pos(1); y_norm = (y_data - ax.YLim(1)) / diff(ax.YLim) * pos(4) + pos(2); end
Usage:
ax = gca; [x_start_norm, y_start_norm] = dsxy2figxy(ax, 3, 0.5); % Data point (3, 0.5) [x_end_norm, y_end_norm] = dsxy2figxy(ax, 4, 0.8); % Arrow tip at (4, 0.8) annotation('textarrow', [x_start_norm, x_end_norm], [y_end_norm, y_start_norm], ... 'String', 'Important Point');
4. Customizing the Arrow
Modify properties like color, head size, and text alignment:
annotation('textarrow', [0.3, 0.4], [0.5, 0.6], ... 'String', 'Note This!', ... 'FontSize', 12, ... 'Color', 'green', ... 'HeadWidth', 15, ... % Arrowhead width 'HeadLength', 20, ... % Arrowhead length 'TextEdgeColor', 'none', ... % Remove text box border 'TextBackgroundColor', [1 1 1 0.5]); % Semi-transparent white background
5. Using gtext
for Interactive Placement
For manual placement during runtime:
gtext('Drag to Position', 'FontWeight', 'bold', 'Color', 'magenta');
- A crosshair will appear; click to place the text arrow.
Key Tips
- Normalized Coordinates:
[0, 1]
for the entire figure window (bottom-left to top-right). - Data Coordinates: Use
textarrow
or convert data points to normalized units. - Alignment: Use
HorizontalAlignment
andVerticalAlignment
to adjust text position relative to the arrow. - Overlap Issues: Set
TextBackgroundColor
to make text readable.
Example Workflow
% Plot data x = 1:10; y = rand(1,10); plot(x, y, 'o-'); % Add text arrow pointing to a specific data point annotation('textarrow', [0.5, 0.55], [0.6, 0.7], ... 'String', 'Outlier', ... 'Color', 'red', ... 'FontSize', 10); % Add another arrow using data coordinates ax = gca; [x1, y1] = dsxy2figxy(ax, 8, y(8)); % Data point (8, y(8)) [x2, y2] = dsxy2figxy(ax, 7.5, y(8)+0.1); annotation('textarrow', [x1, x2], [y1, y2], ... 'String', 'High Value', ... 'HeadWidth', 20);
Troubleshooting
- Arrow Not Visible: Check if coordinates are outside the figure bounds.
- Text Overlaps Plot: Adjust the arrow position or use a semi-transparent background.
For more details, see MATLAB’s documentation on annotation
or textarrow
.