I often struggled to make my plots look professional when I first started working with Python and Matplotlib. The data was there, but the charts didn’t feel polished.
One simple trick that made a big difference was adding horizontal grid lines. These lines make a chart easier to read, especially when you’re comparing values across categories or time.
In this tutorial, I’ll share multiple ways to add horizontal grid lines in Matplotlib. I’ll walk you through each method step by step, using practical examples that you can try right away.
Methods to Add Horizontal Grid Lines in Python Matplotlib
When you’re presenting data, clarity is everything. Horizontal grid lines help users quickly align values with the y-axis.
For example, imagine showing monthly sales data for a U.S. retail store. Without grid lines, it’s harder to compare January sales to July sales. With grid lines, the comparison becomes effortless.
Method 1 – Add Horizontal Grid Lines Using plt.grid()
The easiest way to add horizontal grid lines in Matplotlib is by using the plt.grid() function.
This method is quick and works well for simple plots.
import matplotlib.pyplot as plt
# Sample data: Monthly sales in USD for a U.S. retail store
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
sales = [12000, 15000, 17000, 13000, 18000, 21000]
plt.plot(months, sales, marker='o', linestyle='-', color='blue')
# Add horizontal grid lines only
plt.grid(axis='y', linestyle='--', color='gray', alpha=0.7)
plt.title("Monthly Sales in USA Retail Store")
plt.xlabel("Month")
plt.ylabel("Sales (USD)")
plt.show()I executed the above example code and added the screenshot below.

In this example, I used axis=’y’ to make sure only horizontal grid lines appear. The dashed style (–) makes them subtle but effective.
Method 2 – Add Horizontal Grid Lines with ax.grid()
When I need more control, I prefer working with the object-oriented approach in Matplotlib. Using ax.grid() gives me the flexibility to customize grid lines for a specific subplot.
import matplotlib.pyplot as plt
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
sales = [12000, 15000, 17000, 13000, 18000, 21000]
fig, ax = plt.subplots()
ax.plot(months, sales, marker='o', color='green')
# Add horizontal grid lines with custom style
ax.grid(axis='y', linestyle=':', linewidth=1.2, color='black')
ax.set_title("Monthly Sales with Horizontal Grid Lines")
ax.set_xlabel("Month")
ax.set_ylabel("Sales (USD)")
plt.show()I executed the above example code and added the screenshot below.

Here, I used dotted grid lines (:) and increased the line width for better visibility. This method is great when you’re creating multiple plots.
Method 3 – Add Horizontal Lines with axhline()
Sometimes, instead of full grid lines, I only want specific horizontal lines at certain values.
For example, I might want to highlight a target sales line at $15,000.
import matplotlib.pyplot as plt
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
sales = [12000, 15000, 17000, 13000, 18000, 21000]
plt.plot(months, sales, marker='o', color='purple')
# Add a horizontal line at target sales value
plt.axhline(y=15000, color='red', linestyle='--', linewidth=2, label="Target Sales")
plt.title("Sales Performance vs Target")
plt.xlabel("Month")
plt.ylabel("Sales (USD)")
plt.legend()
plt.show()I executed the above example code and added the screenshot below.

This method is useful when you want to emphasize benchmarks or thresholds in your data.
Method 4 – Add Multiple Horizontal Lines with hlines()
If you need several horizontal lines at once, hlines() is the most efficient method. I often use this when I want to highlight quartiles, averages, or other reference values.
import matplotlib.pyplot as plt
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
sales = [12000, 15000, 17000, 13000, 18000, 21000]
plt.plot(months, sales, marker='o', color='blue')
# Add multiple horizontal lines
y_values = [14000, 16000, 20000] # Example thresholds
plt.hlines(y=y_values, xmin=0, xmax=len(months)-1, colors=['red','orange','green'], linestyles='dashed')
plt.title("Sales with Multiple Reference Lines")
plt.xlabel("Month")
plt.ylabel("Sales (USD)")
plt.show()I executed the above example code and added the screenshot below.

With hlines(), I can add multiple horizontal grid-like lines in one command. This is efficient and keeps the code clean.
Method 5 – Customize Horizontal Grid Lines with Transparency
Sometimes, bold grid lines can overwhelm the chart. That’s when I use transparency (alpha) to keep them subtle.
import matplotlib.pyplot as plt
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
sales = [12000, 15000, 17000, 13000, 18000, 21000]
plt.plot(months, sales, marker='o', color='darkblue')
# Add light horizontal grid lines
plt.grid(axis='y', linestyle='-', color='gray', alpha=0.3)
plt.title("Monthly Sales with Subtle Grid Lines")
plt.xlabel("Month")
plt.ylabel("Sales (USD)")
plt.show()This technique is perfect for presentations where you want the data to stand out more than the grid.
Method 6 – Horizontal Grid Lines in Subplots
When I’m working with multiple subplots, I like to keep grid lines consistent. Here’s how I apply horizontal grid lines to all subplots.
import matplotlib.pyplot as plt
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
sales_store1 = [12000, 15000, 17000, 13000, 18000, 21000]
sales_store2 = [10000, 14000, 16000, 12000, 17000, 19000]
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(8, 6))
ax1.plot(months, sales_store1, marker='o', color='blue')
ax1.set_title("Store 1 Sales")
ax1.grid(axis='y', linestyle='--', alpha=0.6)
ax2.plot(months, sales_store2, marker='o', color='green')
ax2.set_title("Store 2 Sales")
ax2.grid(axis='y', linestyle='--', alpha=0.6)
plt.tight_layout()
plt.show()This way, both charts look consistent and easy to compare.
Practical Use Cases for Horizontal Grid Lines in Python
- Finance: Compare stock prices across months.
- Retail: Track sales performance in U.S. stores.
- Education: Show test score distributions.
- Healthcare: Visualize patient data trends.
In all these cases, horizontal grid lines make the chart easier to interpret.
I’ve shown you several ways to add horizontal grid lines in Matplotlib using Python. From quick methods like plt.grid() to more advanced ones like hlines() and subplot customization, you now have multiple tools at your disposal.
The method you choose depends on your goal. If you just need quick readability, plt.grid(axis=’y’) works fine. If you want more control, ax.grid() or hlines() may be better.
You may also read:
- Add Horizontal Lines with Labels in Python Matplotlib
- Plot Multiple Horizontal Lines in Matplotlib using Python
- Add Horizontal Line in Matplotlib Subplots
- Matplotlib Errorbar with Horizontal Line in Python

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.