HOWTO · Matplotlib

How to Show Pyplot Grid in Matplotlib

Add a grid to a Matplotlib plot with grid(), customize its color and line style, and choose vertical or horizontal grid lines.

On this page

Call grid() on a Matplotlib plot before show() to display grid lines. You can set their color, line style, and width, or use the axis parameter to show vertical lines, horizontal lines, or both. The examples below begin with a line plot, then add and style its grid before comparing the available directions.

Draw a Normal Plot in Matplotlib

Start with x and y values, then pass them to plot(). The labels make each axis clear; this plot is the same one used in the following examples so you can compare the grid changes. Each x value pairs with the y value at the same position, so the lists describe one sequence of points. plot() connects those points in order. The title and labels are optional, but they help identify what the plotted values represent when you adapt the data for your own chart. At this stage the axes contain the line and its ticks, with no grid drawn over the plotting area.

from matplotlib import pyplot as plt

x = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110]
y = [200, 300, 300, 350, 380, 450, 500, 500, 520, 525, 530]

plt.title("My Plot")
plt.xlabel("X Axis")
plt.ylabel("Y Axis")
plt.plot(x, y)
plt.show()

Normal Plot Without Grid in Matplotlib

Use grid() on a Plot in Matplotlib

Call pyplot.grid() after plotting the data and before show(). This adds the grid to the current axes; it does not require displaying the plot first. In this pyplot example, plot() creates the line on the active axes and grid() turns on its grid lines. Keeping both calls before show() lets Matplotlib render the completed figure once. If you already have a plot, add the grid call after the plotting commands and before the display call; the data and labels do not need to be recreated. The grid follows the axes’ tick positions, which makes it easier to estimate values from the line without changing the plotted data.

from matplotlib import pyplot as plt

x = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110]
y = [200, 300, 300, 350, 380, 450, 500, 500, 520, 525, 530]

plt.title("My Plot")
plt.xlabel("X Axis")
plt.ylabel("Y Axis")
plt.plot(x, y)
plt.grid()
plt.show()

Simple Grid in Matplotlib

Change the Grid Attributes in Matplotlib

Pass styling options to grid(). For example, color="r" selects red, linestyle="dotted" draws dotted lines, and linewidth=1 sets a thin line. The value for linewidth can be a number, including a float. The short color codes and line-style names below are common choices; they are examples rather than an exhaustive list. These keyword arguments can be combined in one call, so you can tune the grid without changing the line data or the plot itself. A lighter or thinner grid can keep the data line prominent, while a contrasting color can make the reference lines easier to distinguish. Choose values that remain legible against the axes and any other series in the figure.

from matplotlib import pyplot as plt

x = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110]
y = [200, 300, 300, 350, 380, 450, 500, 500, 520, 525, 530]

plt.title("My Plot")
plt.xlabel("X Axis")
plt.ylabel("Y Axis")
plt.plot(x, y)
plt.grid(color="r", linestyle="dotted", linewidth=1)
plt.show()

Grid Attributes in Matplotlib

Common single-letter color codes include b (blue), g (green), r (red), c (cyan), m (magenta), y (yellow), k (black), and w (white). Common line styles include - or solid, -- or dashed, -. or dashdot, and : or dotted. linewidth controls thickness independently of color and pattern, and accepts numeric values such as 0.5 or 1.5. The example uses red dotted lines with a width of one. Matplotlib also accepts named colors and other line-style forms; consult the linked API reference when you need a specific option or more detail about supported values.

Draw Either Vertical or Horizontal Lines

The axis parameter selects which grid direction to draw: axis="x" draws vertical grid lines at x-axis positions, axis="y" draws horizontal grid lines at y-axis positions, and axis="both" draws both. both is the default. This example places the three settings side by side for comparison. The naming follows the axis whose tick positions control the grid: x ticks sit along the bottom and top of a plot, so their grid lines run vertically; y ticks sit along the sides, so their grid lines run horizontally. Use axis="x" when vertical references are useful, such as comparing values at several x positions. Use axis="y" to compare heights across the chart. Leave the default or write axis="both" when readers need to track both coordinates. Each subplot has its own Axes object, so the loop applies one setting to each panel while reusing the same data.

import matplotlib.pyplot as plt

x = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110]
y = [200, 300, 300, 350, 380, 450, 500, 500, 520, 525, 530]

_, (horizontal, vertical, both) = plt.subplots(1, 3)

for axis, setting in ((horizontal, "y"), (vertical, "x"), (both, "both")):
    axis.plot(x, y)
    axis.grid(axis=setting, linestyle="dotted", color="b")
    axis.set_title(f'axis="{setting}"')

plt.show()

X-axis Y-axis Grid in Matplotlib