Grid lines in matplotlib

Adding grid lines in matplotlib with the grid function

How to add a grid onto a matplotlib chart?

If you want to display grid lines in your plot you will need to call the grid function from matplotlib after creating a plot.

import numpy as np
import matplotlib.pyplot as plt

# Sample data and plot
x = [1, 2, 3, 4, 5]
y = [1, 2, 3, 4, 5]

fig, ax = plt.subplots()
ax.scatter(x, y)

# Enabling grid lines:
ax.grid()

# plt.show()

The grid function in matplotlib pyplot

Grid for the X-axis

The grid lines are added by default both for the X and Y axes, but it is possible to set the grid only for one of them. On the one hand, if you want to add the grid lines for the X axis you can set the axis argument to "x".

import numpy as np
import matplotlib.pyplot as plt

# Sample data and plot
x = [1, 2, 3, 4, 5]
y = [1, 2, 3, 4, 5]

fig, ax = plt.subplots()
ax.scatter(x, y)

# Enabling X-axis grid lines:
ax.grid(axis = "x")

# plt.show()

Grid line for the X-axis in Python

Grid for the Y-axis

On the other hand, if you want to display the grid lines for the Y-axis just set the axis argument to "y".

import numpy as np
import matplotlib.pyplot as plt

# Sample data and plot
x = [1, 2, 3, 4, 5]
y = [1, 2, 3, 4, 5]

fig, ax = plt.subplots()
ax.scatter(x, y)

# Enabling Y-axis grid lines:
ax.grid(axis = "y")

# plt.show()

Grid line for the Y-axis in Python

Grid lines below points

In the previous examples the grid lines were added over the points. However, it is possible to add the lines behind the points just setting set_axisbelow to True.

import numpy as np
import matplotlib.pyplot as plt

# Sample data and plot
x = [1, 2, 3, 4, 5]
y = [1, 2, 3, 4, 5]

fig, ax = plt.subplots()
ax.scatter(x, y)

# Enabling grid lines:
ax.grid()
ax.set_axisbelow(True)

# plt.show()

Grid lines below points in Python with matplotlib and set_axisbelow

Major grid

The grid function provides an argument named which. By setting this argument to "major" all the styling will be applied only to the major grid.

import numpy as np
import matplotlib.pyplot as plt

# Sample data and plot
x = [1, 2, 3, 4, 5]
y = [1, 2, 3, 4, 5]

fig, ax = plt.subplots()
ax.scatter(x, y)

# Enabling grid lines:
ax.grid(which = "major")

# plt.show()

Major grid in matplotlib

Minor grid

If you set the which argument to "minor" you will be able to customize the settings of the minor grid. Nonetheless, unless you call minorticks_on the minor grid won’t show up.

import numpy as np
import matplotlib.pyplot as plt

# Sample data and plot
x = [1, 2, 3, 4, 5]
y = [1, 2, 3, 4, 5]

fig, ax = plt.subplots()
ax.scatter(x, y)

# Enabling minor grid lines:
ax.grid(which = "minor")
ax.minorticks_on()

# plt.show()

Minor grid in matplotlib with minorticks_on

Minor grid without minor ticks

In the previous example, when using the minorticks_on function, the minor ticks will be displayed along the axes. If you prefer not to show those ticks you can remove them with the following line of code:

import numpy as np
import matplotlib.pyplot as plt

# Sample data and plot
x = [1, 2, 3, 4, 5]
y = [1, 2, 3, 4, 5]

fig, ax = plt.subplots()
ax.scatter(x, y)

# Enabling minor grid lines:
ax.grid(which = "minor")
ax.minorticks_on()
ax.tick_params(which = "minor", bottom = False, left = False)

# plt.show()

Minor grid in Python without tick marks

Minor and major grid at the same time

Setting which to both will display both the minor and major grid as long as you also call the minorticks_on function. An alternative is to add each grid individually, which will allow you to set different settings for each grid.

import numpy as np
import matplotlib.pyplot as plt

# Sample data and plot
x = [1, 2, 3, 4, 5]
y = [1, 2, 3, 4, 5]

fig, ax = plt.subplots()
ax.scatter(x, y)

# Enabling both grid lines:
ax.grid(which = "both")
ax.minorticks_on()
ax.tick_params(which = "minor", bottom = False, left = False)

# Alternative:
# ax.grid(which = "major")
# ax.grid(which = "minor")
# ax.minorticks_on()

# plt.show()

Minor and major grid in Python at the same time

Grid style customization

The properties of the grid can be customized through several parameters. Note that you can apply these styling for the whole grid or create several grids (with axis or which) and apply the desired customizations for each grid.

Grid lines color

The default color of the grid lines can be overriden by setting a new color to the color argument

Color of the grid in matplotlib

import numpy as np
import matplotlib.pyplot as plt

# Sample data and plot
x = [1, 2, 3, 4, 5]
y = [1, 2, 3, 4, 5]

fig, ax = plt.subplots()
ax.scatter(x, y)

# Red grid
ax.grid(color = "red")

# plt.show()

Grid lines style

The style or type of line used to create the grid can be customized with the linestyle or ls argument. In the following example we are setting a dashed line.

Line style of the grid lines in Python

import numpy as np
import matplotlib.pyplot as plt

# Sample data and plot
x = [1, 2, 3, 4, 5]
y = [1, 2, 3, 4, 5]

fig, ax = plt.subplots()
ax.scatter(x, y)

# Grid line style
ax.grid(linestyle = "dashed")

# plt.show()

Grid lines width

The width of the grid lines can be customized both for major and minor grid lines. If you use both grids, the most common practice is to set the major grid wider than the minor grid. The linewidth or lw argument can be used for this purpose.

Line width of the grids in matplitlib

import numpy as np
import matplotlib.pyplot as plt

# Sample data and plot
x = [1, 2, 3, 4, 5]
y = [1, 2, 3, 4, 5]

fig, ax = plt.subplots()
ax.scatter(x, y)

# Grid line width
ax.grid(which = "major", linewidth = 1)
ax.grid(which = "minor", linewidth = 0.2)
ax.minorticks_on()

# plt.show()

Lines transparency

The alpha argument controls the transparency of the grid lines. This argument is very interesting if you want faded grid lines.

Transparency of the grid lines in matplotlib pyplot

import numpy as np
import matplotlib.pyplot as plt

# Sample data and plot
x = [1, 2, 3, 4, 5]
y = [1, 2, 3, 4, 5]

fig, ax = plt.subplots()
ax.scatter(x, y)

# Grid with transparency
ax.grid(linewidth = 1.5, alpha = 0.25)

# plt.show()

Custom grid lines position

Sometimes, when you create a chart with a grid you want to customize the position of the grid lines. You can accomplish this with FixedLocator and AutoMinorLocator from matplotlib.ticker and with the set_[major|minor]_locator functions.

Consider, for instance, that you want to set the X-axis major grid only for X = [1, 3, 5]. In this scenario, you will need to use the FixedLocator and xaxis.set_major_locator. The same can be applied to the Y-axis and the minor grid. However, you could also use the AutoMinorLocator function to create N-1 ticks equally spaced between the major ticks. See the example below for clarification.

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import AutoMinorLocator, FixedLocator

# Sample data and plot
x = [1, 2, 3, 4, 5]
y = [1, 2, 3, 4, 5]

fig, ax = plt.subplots()
ax.scatter(x, y)

# Enabling both grids:
ax.grid(which = "major")
ax.grid(which = "minor", alpha = 0.2)

# Locations for the X-axis, major grid
ax.xaxis.set_major_locator(FixedLocator([1, 3, 5]))

# Locations for the Y-axis, major grid
ax.yaxis.set_major_locator(FixedLocator([2, 4]))

# Minor X-axis divided into 10 parts between each X-axis major grid
ax.xaxis.set_minor_locator(AutoMinorLocator(10))

# plt.show()

Set the grid lines position in Python with matplotlib with FixedLocator

Storytelling with Data

A Data Visualization Guide for Business Professionals

Buy on Amazon
Data Sketches

A journey of imagination, exploration, and beautiful data visualizations

Buy on Amazon

See also