简体   繁体   English

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

[英]Matplotlib Scatter plot filter color (Colorbar)

I have some data let say x , y , and z . 我有一些数据,可以说xyz All are 1D arrays. 所有都是1D数组。 I have done a scatter plot with z as color as; 我用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() 

The z values are normalized and its between -1 to 1 . z值已归一化,介于-11之间。 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). 我该如何过滤颜色,使得颜色值介于-0.250.25之间的点从图形中消失(即,将颜色设置为白色)。

在此处输入图片说明

The values for x , y , and z can be provided if needed to answer this question. 如果需要回答此问题,可以提供xyz的值。 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. 右侧的图将隐藏所有低于所选阈值的点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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