15.1 C
London
Friday, July 5, 2024
HomePythonMatplotlib in PythonHow to Plot Only Horizontal Gridlines in Matplotlib

How to Plot Only Horizontal Gridlines in Matplotlib

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

You can use the following basic syntax to only plot horizontal gridlines in Matplotlib:

ax.grid(axis='y')

The following example shows how to use this syntax in practice.

Example: Plot Only Horizontal Gridlines in Matplotlib

The following code shows how to create a bar plot in Matplotlib with only horizontal gridlines shown in the plot:

import pandas as pd
import matplotlib.pyplot as plt

#create DataFrame
df = pd.DataFrame({'team':['Mavs', 'Nets', 'Spurs', 'Warriors'],
                   'points':[105, 99, 112, 100]})

#define plot
fig, ax = plt.subplots()

#create bar plot
df.plot(kind='bar', ax=ax)

#add horizontal gridlines
ax.grid(axis='y')

#display plot
plt.show()

Feel free to use ax.set_axisbelow(True) to display the horizontal gridlines behind the bars in the plot:

import pandas as pd
import matplotlib.pyplot as plt

#create DataFrame
df = pd.DataFrame({'team':['Mavs', 'Nets', 'Spurs', 'Warriors'],
                   'points':[105, 99, 112, 100]})

#define plot
fig, ax = plt.subplots()

#create bar plot
df.plot(kind='bar', ax=ax)

#add horizontal gridlines behind bars in the plot
ax.set_axisbelow(True)
ax.grid(axis='y')

#display plot
plt.show()

Matplotlib horizontal gridlines

And feel free to use the color, linestyle, and linewidth arguments within the grid() function to customize the appearance of the gridlines:

import pandas as pd
import matplotlib.pyplot as plt

#create DataFrame
df = pd.DataFrame({'team':['Mavs', 'Nets', 'Spurs', 'Warriors'],
                   'points':[105, 99, 112, 100]})

#define plot
fig, ax = plt.subplots()

#create bar plot
df.plot(kind='bar', ax=ax)

#add horizontal gridlines with custom appearance
ax.set_axisbelow(True)
ax.grid(axis='y', color='red', linestyle='dashed', linewidth=3)

#display plot
plt.show()

You can find a complete list of ways to customize the gridlines in the Matplotlib documentation.

Additional Resources

The following tutorials explain how to perform other common tasks in Matplotlib:

How to Remove Ticks from Matplotlib Plots
How to Change Font Sizes on a Matplotlib Plot
How to Add an Average Line to Plot in Matplotlib

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories