Matplotlib散点图过滤器颜色(Colorbar) [英] Matplotlib Scatter plot filter color (Colorbar)

查看:58
本文介绍了Matplotlib散点图过滤器颜色(Colorbar)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些数据,例如 x y z .全部都是 1D 数组.我用 z 作为颜色做了一个散点图;

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

z 值是标准化的,它介于 -11 之间.我已附上下图.

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

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

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 (i.e set the color to white).

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

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)

然后进行绘图:

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)

给出以下结果:

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

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

这篇关于Matplotlib散点图过滤器颜色(Colorbar)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆