Skip to content
Home » matplotlib Part 12 – Grids

matplotlib Part 12 – Grids

Spread the love

Grids

In the previous part of the Matplotlib series we were talking about axis ticks. Today’s topic is closely related to this. A graph will become more readable if you add grid lines to it.

Grid lines are sort of extensions of the ticks on the graph, they are drawn at the same coordinates.

To demonstrate how grids work, let’s use the example from the previous lecture. Here it is without grid lines:

In [1]:
%matplotlib inline
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(1, 100, 300)
y = np.sin(x) / x

fig, ax = plt.subplots(figsize=(15, 5))
ax.plot(x, y)

# Set 6 major ticks and 30 minor ticks at unspecified locations.
ax.xaxis.set_major_locator(mpl.ticker.MaxNLocator(6))
ax.xaxis.set_minor_locator(mpl.ticker.MaxNLocator(30))

It’s very easy to turn on a basic grid. All you need to do is call the grid method without any arguments. The grid lines will be drawn at the coordinates of the major ticks:

In [2]:
fig, ax = plt.subplots(figsize=(15, 5))
ax.plot(x, y)

ax.xaxis.set_major_locator(mpl.ticker.MaxNLocator(6))
ax.xaxis.set_minor_locator(mpl.ticker.MaxNLocator(30))

# Let's draw a basic grid.
ax.grid()

You can also pass keyword arguments to the grid method to tweak the grid. Here you can see some of them in action:

In [4]:
fig, ax = plt.subplots(figsize=(15, 5))
ax.plot(x, y)

ax.xaxis.set_major_locator(mpl.ticker.MaxNLocator(6))
ax.xaxis.set_minor_locator(mpl.ticker.MaxNLocator(30))

# Let's set the grid's color, linestyle and linewidth.
ax.grid(color='red', linestyle=':', linewidth=2)

You can also pass some other arguments, like:

  • which – set to ‘major’, ‘minor’ or ‘both’ to indicate which ticks to use,
  • axis – set to ‘x’, ‘y’ or ‘both’ to indicate which axis to use.

Here you can see them in action:

In [7]:
fig, ax = plt.subplots(figsize=(15, 5))
ax.plot(x, y)

ax.xaxis.set_major_locator(mpl.ticker.MaxNLocator(6))
ax.xaxis.set_minor_locator(mpl.ticker.MaxNLocator(30))

# Let's set the grid lines on both major and minor ticks only on the X axis.
ax.grid(which='both', axis='x')
In [8]:
fig, ax = plt.subplots(figsize=(15, 5))
ax.plot(x, y)

ax.xaxis.set_major_locator(mpl.ticker.MaxNLocator(6))
ax.xaxis.set_minor_locator(mpl.ticker.MaxNLocator(30))

# Let's set individual grid lines for:

# 1) the major ticks on the X axis
ax.grid(which='major', axis='x', color='blue', linewidth=2)

# 2) the minor ticks on the X axis
ax.grid(which='minor', axis='x', color='blue', linewidth=1, linestyle=':')

# 3) the major ticks on the Y axis
ax.grid(which='major', axis='y', color='green')

Here’s another example from the previous lecture:

In [9]:
fig, ax = plt.subplots(figsize=(15, 5))
ax.plot(x, y)

ax.xaxis.set_major_locator(mpl.ticker.FixedLocator([6, 18, 44, 50, 82]))
ax.xaxis.set_minor_locator(mpl.ticker.MultipleLocator(2))
ax.yaxis.set_major_locator(mpl.ticker.MaxNLocator(3))
ax.yaxis.set_minor_locator(mpl.ticker.MultipleLocator(.1))

Let’s add a grid to it as well:

In [12]:
fig, ax = plt.subplots(figsize=(15, 5))
ax.plot(x, y)

ax.xaxis.set_major_locator(mpl.ticker.FixedLocator([6, 18, 44, 50, 82]))
ax.xaxis.set_minor_locator(mpl.ticker.MultipleLocator(2))
ax.yaxis.set_major_locator(mpl.ticker.MaxNLocator(3))
ax.yaxis.set_minor_locator(mpl.ticker.MultipleLocator(.1))

# Let's set individual grid lines for:

# 1) the major ticks on the X axis
ax.grid(which='major', axis='x', color='blue', linewidth=2)

# 2) the minor ticks on the X axis
ax.grid(which='minor', axis='x', color='blue', linewidth=1, linestyle=':')

# 3) the major ticks on the Y axis
ax.grid(which='major', axis='y', color='green', linewidth=2)

# 4) the minor ticks on the Y axis
ax.grid(which='minor', axis='y', color='green', linewidth=1, linestyle=':')

Your Panda3D Magazine

Make Awesome Games and Other 3D Apps

with Panda3D and Blender using Python.

Cool stuff, easy to follow articles.

Get the magazine here (PDF).

Python Jumpstart Course

Learn the basics of Python, including OOP.

with lots of exercises, easy to follow

The course is available on Udemy.

Blender Jumpstart Course

Learn the basics of 3D modeling in Blender.

step-by-step, easy to follow, visually rich

The course is available on Udemy and on Skillshare.


Spread the love

Leave a Reply