Skip to content Skip to sidebar Skip to footer

Matplotlib Scatter Plot Filter Color (colorbar)

I have some data let say x, y, and z. All are 1D arrays. I have done a scatter plot with z as color as; import matplotlib.pyplot as plt plt.scatter(x,y,c=z,alpha = 0.2) plt.xlab

Solution 1:

import matplotlib.pyplot as plt
import numpy as np
np.random.seed(42)

# prepare random data
stats = -1, 1, 200
x = np.random.uniform(*stats)
y = np.random.uniform(*stats)
z = np.random.uniform(*stats)

# mask unwanted data
thresh = 0.4
mask = np.abs(z) <= thresh
x_ma = np.ma.masked_where(mask, x)
y_ma = np.ma.masked_where(mask, y)
z_ma = np.ma.masked_where(mask, z)

And do the plotting:

fig, (ax_left, ax_right) = plt.subplots(1, 2, figsize=(10, 4),
                                        sharex=True, sharey=True)
img_left = ax_left.scatter(x, y, c=z)
fig.colorbar(img_left, ax=ax_left)
img_right = ax_right.scatter(x_ma, y_ma, c=z_ma)
fig.colorbar(img_right, ax=ax_right)

gives following result:

plot

The plot on the right side hides all points that fall below the chosen threshold.

Post a Comment for "Matplotlib Scatter Plot Filter Color (colorbar)"