简体   繁体   中英

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.xlabel("X")
 plt.ylabel("Y")
 plt.ylim((1.2,1.5))
 plt.colorbar() 

The z values are normalized and its between -1 to 1 . I have attached the figure below.

The question I have is; How can I filter the colors such that let say the points that have a color value between -0.25 to 0.25 disappear form the figure (ie set the color to white).

在此处输入图片说明

The values for x , y , and z can be provided if needed to answer this question. Thank you for your time.

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:

情节

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM