Matplotlib – Set the line properties for Grid

In Matplotlib, set the line properties for the grid using the linestyle and linewidth properties of the grid() function.

Key options

linestyle:

  • ‘-‘ → solid
  • ‘–‘ → dashed
  • ‘:’ → dotted
  • ‘-.’ → dash-dot

linewidth:

Controls the thickness of the grid lines (e.g., 0.5, 1, 2).

Let us see an example:

# Set line properties for Grid

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(color = 'grey', linestyle = '--', linewidth = 1.5)
plt.show()

Output

Set line properties for Grid 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 - Add a super title for multiple plots in a figure
Matplotlib - Add Grid Lines on x or y axis
Studyopedia Editorial Staff
contact@studyopedia.com

We work to create programming tutorials for all.

No Comments

Post A Comment