Change the scatter plot marker opacity in Matplotlib

Python

Matplotlib

Scatter

Change marker opacity

Luc B.

Python

Matplotlib

Scatter Plot

When a scatter plot has a lot of points, markers overlap and blend into a blob. This can be circumvented by adding transparency to the markers, causing regions with lots of overlapping markers to appear darker.

Code Example

Use the alpha parameter to plt.scatter() to make translucent markers. In the example below, we color the markers gray by setting the c parameter to a string value between 0 and 1.

import matplotlib.pyplot as plt
import numpy as np

vals = np.random.multivariate_normal([0, 0], [[4, 2], [2, 3]], 2000)

# Create a scatter plot with translucent markers.
plt.scatter(vals[:, 0], vals[:, 1], alpha=0.2, c='0.7')

plt.show()

png