gpt4 book ai didi

python - 如何根据 matplotlib 中的一些规则为数据点着色

转载 作者:行者123 更新时间:2023-12-03 21:41:55 25 4
gpt4 key购买 nike

我有一个信号,我想用离信号平均值太远的红点着色。例如:

k=[12,11,12,12,20,10,12,0,12,10,11]
x2=np.arange(1,12,1)
plt.scatter(x2,k, label="signal")
plt.show()

我想将数据点 20 和 0 涂成红色,并给它们一个特殊标签,例如“警告”。
我读了 matplotlib: how to change data points color based on some variable ,但我不确定如何将其应用于我的案例

最佳答案

如果你想要不同的标签,你需要不同的图。
根据公式过滤数据。
在这种情况下,我采用了与平均值相差超过 1.5 个标准差的值。如果您不知道,请在 numpy您可以使用 bool 掩码来索引数组,并且只使用掩码为 True 的元素。 .您还可以使用补码运算符 ~ 轻松翻转掩码.

import matplotlib.pyplot as plt
import numpy as np

k=np.array([12,11,12,12,20,10,12,0,12,10,11])
x2=np.arange(1,12,1)

# find out which parameters are more than 1.5*std away from mean
warning = np.abs(k-np.mean(k)) > 1.5*np.std(k)

# enable drawing of multiple graphs on one plot
plt.hold(True)

# draw some lines behind the scatter plots (using zorder)
plt.plot(x2, k, c='black', zorder=-1)

# scatter valid (not warning) points in blue (c='b')
plt.scatter(x2[~warning], k[~warning], label='signal', c='b')

# scatter warning points in red (c='r')
plt.scatter(x2[warning], k[warning], label='warning', c='r')

# draw the legend
plt.legend()

# show the figure
plt.show()

这是你得到的:

enter image description here

关于python - 如何根据 matplotlib 中的一些规则为数据点着色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32653825/

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