python - Matplotlib 散点图过滤器颜色(Colorbar)

标签 python matplotlib scatter-plot

我有一些数据,比如xyz。全部都是1D数组。我已经制作了一个散点图,其中 z 的颜色为;

 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() 

z 值已标准化,其范围在 -11 之间。我附上了下图。

我的问题是;如何过滤颜色,使得颜色值在 -0.250.25 之间的点从图形中消失(即将颜色设置为白色)。

enter image description here

如果需要回答此问题,可以提供 xyz 的值。感谢您的时间。

最佳答案

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)

并进行绘图:

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)

给出以下结果:

plot

右侧的图隐藏了所有低于所选阈值的点。

关于python - Matplotlib 散点图过滤器颜色(Colorbar),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28577924/

相关文章:

python - 开机后自动运行 python 脚本?

android - 有没有办法在Android上运行Python?

python - 带有标题的 plt.figure() 给出错误 : invalid literal for int() with base 10

python - 将 matplotlib 图保存为数据库中的图像字段

python - python easy install 的问题

java - selenium 服务器,selenium 客户端,在 UBUNTU GUI 服务器上

python - raw_input 在请求输入后打印提示

c++ - 使用VTK绘制不同颜色的点

python-3.x - 使用 matplotlib 自动为散点图指定颜色?

python - 散点图上的 Bokeh 图回归线