gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 04:50:32 32 4
gpt4 key购买 nike

我有一些数据,比如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/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com