Matplotlib

Grid Lines


Grid lines in matplotlib

This page covers a matplotlib grid lines tutorial.

In certain situations, grid lines can be very beneficial. Grid lines are lines that cross the plot, horizontally or vertically or both. They show the division of axes and help readers analyse data more effectively. The following are some of the benefits grid lines offer:

  1. Visual alignment – help align data points and make it easier to interpret the relationship between them.
  2. Data reading – facilitate reading and interpreting data values from the chart accurately.
  3. Chart organisation – help in organizing the chart layout, especially when dealing with multiple data series or complex plots.
  4. Data comparison – aid in comparing data across different categories or time periods.
  5. Scale interpretation – help users understand the scale of the chart axes, especially in logarithmic or non-linear scales.
  6. Chart aesthetics – enhance the overall aesthetics of the chart by providing structure and balance.
  7. Communication clarity – improve the clarity of the chart by making it easier to communicate key insights and messages.

Implementing grid lines in matplotlib is quite smooth.

The library uses the function grid(). Let’s explore the technique through an example.

import matplotlib.pyplot as plt

# Sample data 1
x = [1, 4, 7, 9]
y = [2, 3, 7, 6]

# Sample data 2
a = [3, 2, 6, 7]
b = [1, 3, 5, 9]

# Plot the data
plt.plot(x, y)
plt.plot(a, b)

# Grid
plt.grid()

# Display the plot
plt.show()
matplotlib-grid

Axis grid lines

We can also specify the axis lines. This is done through the argument axis, which requires either x or y.

We look at both scenarios separately.

import matplotlib.pyplot as plt

# Sample data 1
x = [1, 4, 7, 9]
y = [2, 3, 7, 6]

# Sample data 2
a = [3, 2, 6, 7]
b = [1, 3, 5, 9]

# Plot the data
plt.plot(x, y)
plt.plot(a, b)

# Grid
plt.grid(axis="x")

# Display the plot
plt.show()
matplotlib-gridlines-x-axis
import matplotlib.pyplot as plt

# Sample data 1
x = [1, 4, 7, 9]
y = [2, 3, 7, 6]

# Sample data 2
a = [3, 2, 6, 7]
b = [1, 3, 5, 9]

# Plot the data
plt.plot(x, y)
plt.plot(a, b)

# Grid
plt.grid(axis="y")

# Display the plot
plt.show()
matplotlib-gridlines-y-axis

Line customisation

Customising the lines of a grid can also aid its visualisation. Similarly to the data lines, grid lines have properties. Such properties can be adjusted.

Matplotlib applies the same methods to grid lines as the ones with data lines. They are: linestyle, linewidth, and color. Matplotlib provides shortcuts for many of its tools.

  • linestyle (or ls) – adjusts the style of the lines, such as dashed (“–“) or dotted (“:”).
  • linewidth (or lw) – adjusts the size (width) of the lines.
  • color (or c) – adjusts the colour of the lines, such as “green”, “g”, or “#008000”

Let’s explore the above with an example.

import matplotlib.pyplot as plt

# Sample data 1
x = [1, 4, 7, 9]
y = [2, 3, 7, 6]

# Sample data 2
a = [3, 2, 6, 7]
b = [1, 3, 5, 9]

# Plot the data
plt.plot(x, y)
plt.plot(a, b)

# Grid
plt.grid(ls=":", linewidth=2.5, c="y")

# Display the plot
plt.show()
matplotlib-grid-lines-customisation

This is an original grid lines tutorial. Created by aicorr.com.

Next: Line Plots