Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How to set labels in matplotlib.hlines?
The matplotlib.hlines() function draws horizontal lines across the plot. To add labels to these lines, you can use the plt.text() method or create a legend by combining multiple hlines() calls with label parameters.
Method 1: Using plt.text() for Direct Labels
This approach places text labels directly next to the horizontal lines ?
import matplotlib.pyplot as plt # Set the figure size plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True # Add horizontal line with direct text label plt.hlines(y=1, xmin=1, xmax=4, lw=7, color='orange') plt.text(4, 1, 'y=1', ha='left', va='center', fontsize=12) # Add another horizontal line with direct text label plt.hlines(y=2, xmin=2, xmax=5, lw=7, color='red') plt.text(2, 2, 'y=2', ha='right', va='center', fontsize=12) plt.xlim(0, 6) plt.ylim(0, 3) plt.show()
# Displays a plot with two horizontal lines and their labels positioned directly on the plot
Method 2: Using Labels with Legend
For a cleaner approach, use the label parameter with plt.legend() ?
import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True # Add horizontal lines with labels for legend plt.hlines(y=1, xmin=1, xmax=4, lw=7, color='orange', label='Line at y=1') plt.hlines(y=2, xmin=2, xmax=5, lw=7, color='red', label='Line at y=2') # Display legend plt.legend() plt.xlim(0, 6) plt.ylim(0, 3) plt.show()
# Displays a plot with two horizontal lines and a legend box showing the labels
Method 3: Combining Text Labels with Formatting
You can enhance labels with custom formatting and positioning ?
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = [8.00, 4.00]
plt.rcParams["figure.autolayout"] = True
# Add horizontal lines
plt.hlines(y=1.5, xmin=0.5, xmax=3.5, lw=5, color='blue', alpha=0.7)
plt.hlines(y=2.5, xmin=1.5, xmax=4.5, lw=5, color='green', alpha=0.7)
# Add formatted text labels
plt.text(3.7, 1.5, 'Baseline (y=1.5)', ha='left', va='center',
fontsize=10, bbox=dict(boxstyle='round', facecolor='lightblue'))
plt.text(1.3, 2.5, 'Target (y=2.5)', ha='right', va='center',
fontsize=10, bbox=dict(boxstyle='round', facecolor='lightgreen'))
plt.xlim(0, 5)
plt.ylim(1, 3)
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Labeled Horizontal Lines')
plt.show()
# Displays a plot with formatted text labels in colored boxes next to horizontal lines
Comparison
| Method | Best For | Advantages |
|---|---|---|
plt.text() |
Direct labeling | Precise positioning, custom formatting |
label + legend() |
Multiple lines | Clean appearance, automatic positioning |
| Formatted text boxes | Professional plots | Enhanced visibility, custom styling |
Conclusion
Use plt.text() for direct labeling with precise control over positioning. For multiple horizontal lines, combine label parameters with plt.legend() for a cleaner appearance.
Advertisements
