Change the line opacity in Matplotlib




Change opacity

Luc B.



Line Plot

Transparency is a valuable tool in plots with oodles of lines, as translucent lines can overlap and constructively interfere to create dark regions that signify repeated trends.

For example, if an analyst wants to compare road traffic trends across different days, they could create a plot with time-of-day on the x-axis and traffic volume on the y-axis, drawing lines that represent traffic trends on individual days. Plotting data from multiple days on the same axes allows them to compare daily trends, which is very useful. However, using this method to compare traffic data for an entire month or year is impractical—the plot becomes a big mess of uninterpretable lines.

This is easily resolved with transparency. As a multitude of translucent traffic trends overlap, they create dark and light regions that represent common and rare daily traffic patterns, respectively.

Code Example

Use the alpha parameter to plt.plot() to change the line opacity in Matplotlib.

import matplotlib.pyplot as plt
import numpy as np

x_values = np.arange(0, 7)
y_values = np.array([0.27, 0.32, 0.35, 0.49, 0.55, 0.7, 0.95])

# Adjust the line transparency here
plt.plot(x_values, y_values, 'b', alpha=0.25)       # 25% Opacity
plt.plot(x_values, y_values - 0.5, 'b', alpha=0.5)  # 50% Opacity
plt.plot(x_values, y_values - 1, 'b', alpha=0.75)   # 75% Opacity

plt.legend(['0.25 Opacity', '0.50 Opacity', '0.75 Opacity'])