Matplotlib Text: Print Labels & Annotations
Learn to use Matplotlib's text() function to add labels, titles, and annotations to your data visualizations. Enhance plots with custom positioning and styling.
Printing Text in Matplotlib
Matplotlib's text capabilities are essential for creating informative and visually appealing data visualizations. The text()
function provides a versatile way to add labels, annotations, titles, and descriptions directly onto your plots.
This document outlines how to use the text()
function to enhance your Matplotlib plots, covering various customization options like positioning, rotation, and enclosing text in boxes.
1. Printing Text on a Plot
The most basic usage of plt.text()
involves specifying the x and y coordinates where the text should be placed, followed by the text string itself.
Example: Adding Text to a Plot
import matplotlib.pyplot as plt
# Create a simple plot
plt.plot([1, 2, 3], [4, 5, 6])
# Add text annotation at coordinates (2, 5)
plt.text(2, 5, 'Hello, Matplotlib!', fontsize=12, color='red')
# Display the plot
plt.show()
Output: This code will display a line plot with the text "Hello, Matplotlib!" positioned at the data coordinates (2, 5). The text will be rendered in red with a font size of 12.
2. Printing Text on a Polar Plot
The text()
function can also be used effectively within polar plots. When using polar plots, you specify the text's position using angular (theta
) and radial (r
) coordinates.
Example: Adding Text to a Polar Plot
import matplotlib.pyplot as plt
import numpy as np
# Create data for a polar plot
theta = np.linspace(0, 2 * np.pi, 100)
r = np.sin(3 * theta)
# Create the polar plot
plt.polar(theta, r)
# Add text annotation on the polar plot
# Positioned at 90 degrees (pi/2 radians) and radius 0.5
# 'ha' (horizontal alignment) is set to 'center'
plt.text(np.pi / 2, 0.5, 'Polar Plot', fontsize=12, color='blue', ha='center')
# Display the plot
plt.show()
Output:
This example generates a polar plot. The text "Polar Plot" is added at an angle of 90 degrees and a radial distance of 0.5. The ha='center'
argument ensures the text is horizontally centered at its specified position.
3. Printing Rotated Text
To improve readability or for stylistic purposes, you can rotate text annotations. The rotation
parameter in plt.text()
allows you to specify the angle of rotation in degrees.
Example: Adding Rotated Text to a Plot
import matplotlib.pyplot as plt
# Create a simple plot
plt.plot([1, 2, 3], [4, 5, 6])
# Add rotated text annotation
# The text is rotated by 45 degrees
plt.text(2, 5, 'Rotated Text', fontsize=12, color='purple', rotation=45)
# Display the plot
plt.show()
Output: The plot will show the line and the text "Rotated Text" at coordinates (2, 5), rotated by 45 degrees counter-clockwise.
4. Printing Text with a Box
You can draw a bounding box around text to make it stand out or to visually group it. This is achieved using the bbox
parameter, which accepts a dictionary of properties to style the box.
Example: Adding Boxed Text to a Plot
import matplotlib.pyplot as plt
# Create a simple plot
plt.plot([1, 2, 3], [4, 5, 6])
# Add text annotation enclosed in a box
plt.text(2, 5, 'Boxed Text', fontsize=12, color='orange',
bbox=dict(facecolor='lightyellow', edgecolor='orange', boxstyle='round,pad=0.5'))
# Display the plot
plt.show()
Output:
This code renders a line plot with the text "Boxed Text" at (2, 5). The text is enclosed in a rounded box with a light yellow background and an orange border. The pad
argument within boxstyle
adds some padding between the text and the box edges.
Conclusion
The matplotlib.pyplot.text()
function is a powerful tool for annotating your plots. It allows you to:
- Add descriptive text and labels to any plot.
- Precisely position text using data coordinates.
- Rotate text for better layout and emphasis.
- Integrate text seamlessly into polar plots.
- Enclose text within customizable bounding boxes for enhanced visibility.
By mastering these features, you can significantly improve the clarity and informative value of your data visualizations.
Matplotlib Keywords: Effortless Plot Customization
Master Matplotlib plotting with keywords for efficient customization. Learn to control color, markers, lines, labels, and titles for clear data visualization.
Ribbon Box Plot: Visualize Data Distribution with Python
Learn how to create powerful ribbon box plots in Python using Matplotlib to visualize and compare data distributions across categories effectively.