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:
%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:
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:
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:
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')
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:
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:
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=':')