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
Plotting a horizontal line on multiple subplots in Python using pyplot
To plot a horizontal line on multiple subplots in Python, we can use subplots() to create multiple axes and the axhline() method to draw horizontal lines across each subplot.
Steps
Create a figure and a set of subplots using
plt.subplots()Use the axhline() method on each axis to draw horizontal lines
Customize line properties like color, width, and position
Display the figure using
plt.show()
Example
Here's how to create three subplots with horizontal lines of different colors and widths −
from matplotlib import pyplot as plt
# Create figure with 3 subplots
fig, (ax1, ax2, ax3) = plt.subplots(3, figsize=(7, 6))
# Draw horizontal lines on each subplot
ax1.axhline(y=0.5, color="black", linewidth=2, label="Black Line")
ax2.axhline(y=0.5, color="red", linewidth=3, label="Red Line")
ax3.axhline(y=0.5, color="blue", linewidth=4, label="Blue Line")
# Add some sample data for context
ax1.plot([0, 1, 2, 3], [0.2, 0.8, 0.3, 0.7], 'o-', alpha=0.7)
ax2.plot([0, 1, 2, 3], [0.1, 0.9, 0.4, 0.6], 's-', alpha=0.7)
ax3.plot([0, 1, 2, 3], [0.3, 0.7, 0.2, 0.8], '^-', alpha=0.7)
# Set titles for each subplot
ax1.set_title("Subplot 1")
ax2.set_title("Subplot 2")
ax3.set_title("Subplot 3")
# Add legends
ax1.legend()
ax2.legend()
ax3.legend()
plt.tight_layout()
plt.show()
Key Parameters of axhline()
y − The y-coordinate where the line will be drawn
xmin, xmax − Start and end points along x-axis (0 to 1 by default)
color − Line color (can use names like 'red' or hex codes)
linewidth − Thickness of the line
linestyle − Style like '--', ':', '-.' etc.
Advanced Example with Custom Positioning
You can also control the horizontal extent of lines using xmin and xmax parameters −
from matplotlib import pyplot as plt
import numpy as np
# Create 2x2 subplots
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, figsize=(10, 6))
# Different horizontal line configurations
ax1.axhline(y=0, color='green', linewidth=2, linestyle='--')
ax1.set_title("Full Width Dashed Line")
ax2.axhline(y=0.5, xmin=0.2, xmax=0.8, color='orange', linewidth=3)
ax2.set_title("Partial Width Line")
ax3.axhline(y=-1, color='purple', linewidth=1)
ax3.axhline(y=1, color='purple', linewidth=1)
ax3.set_title("Multiple Horizontal Lines")
ax4.axhline(y=0, color='red', linewidth=2, alpha=0.5)
ax4.set_title("Semi-transparent Line")
# Add some data to make lines more visible
for ax in [ax1, ax2, ax3, ax4]:
x = np.linspace(-2, 2, 50)
y = np.sin(x)
ax.plot(x, y, 'b-', alpha=0.7)
ax.grid(True, alpha=0.3)
plt.tight_layout()
plt.show()
Conclusion
The axhline() method provides an easy way to add horizontal reference lines across multiple subplots. Use parameters like color, linewidth, and xmin/xmax to customize the appearance and positioning of your horizontal lines.
