Matplotlib 散点图过滤器颜色(Colorbar)
Matplotlib Scatter plot filter color (Colorbar)
我有一些数据可以说 x
、y
和 z
。都是 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
值被归一化,它在 -1
到 1
之间。我附上了下图。
我的问题是;我怎样才能过滤颜色,让颜色值在 -0.25
到 0.25
之间的点从图中消失(即将颜色设置为白色)。
如果需要回答此问题,可以提供 x
、y
和 z
的值。感谢您的时间。
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)
给出以下结果:
右侧的图隐藏了所有低于所选阈值的点。
我有一些数据可以说 x
、y
和 z
。都是 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
值被归一化,它在 -1
到 1
之间。我附上了下图。
我的问题是;我怎样才能过滤颜色,让颜色值在 -0.25
到 0.25
之间的点从图中消失(即将颜色设置为白色)。
如果需要回答此问题,可以提供 x
、y
和 z
的值。感谢您的时间。
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)
给出以下结果:
右侧的图隐藏了所有低于所选阈值的点。