Matplotlib – Add Grid Lines on x or y axis

In Matplotlib, add grid lines on the x or y axis using the axis parameter of the plt.grid() function. Let us see an example:

# Add Grid Lines on x or y axis

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame(
    {
        "Cricket_Bat":['SG', 'BDM', 'SS', 'GM', 'Kookaburra', 'Spartan'],
        "MRP": [2000, 2200, 2400, 2700, 2800, 3000],
        "Weight_Grams": [1100, 1200, 1250, 1330, 1480, 1600]
    }
)

plt.plot(df['MRP'], df['Weight_Grams'])

plt.xlabel('Bat Price (USD)')
plt.ylabel('Bat Weight (Grams)')

plt.title('Bat Price depends on the weight', loc='left')

plt.grid(axis = 'x')
plt.show()

Output

Add Grid Lines on x or y axis in Matplotlib


If you liked the tutorial, spread the word and share the link and our website, Studyopedia, with others.


For Videos, Join Our YouTube Channel: Join Now


Read more:

Matplotlib - Set the line properties for Grid
Studyopedia Editorial Staff
contact@studyopedia.com

We work to create programming tutorials for all.

No Comments

Post A Comment