Matplotlib - Grid Line Width


Matplotlib - Grid Line Width

In Matplotlib, you can choose a specific line width for the grid.

To set a specific line width for the lines in grid, call the grid() function from matplotlib.pyplot and pass required line width (float) value as the argument for the linewidth parameter.

linewidth parameter accepts a floating point value. The default value of line width is 0.5.

For example, plt.grid(linewidth=2.0) displays the grid with a line width of 2.0 units, which is four times the default line width.

import matplotlib.pyplot as plt
plt.grid(linewidth=2.0)

Examples

1. Grid line width of 2.0 in Matplotlib

In the following program, we shall set the grid line width to 2.0.

Python Program

import matplotlib.pyplot as plt

# Example data
x = [1, 2, 3, 4, 5]
y = [10, 20, 25, 55, 40]

# Plot the data
plt.plot(x, y, marker='o')

# Grid with specific line width
plt.grid(linewidth=2.0)

# Show the plot
plt.show()

Output

Matplotlib - Grid Line Width example
Grid linewidth=2.0