gpt4 book ai didi

python - 如何屏蔽线以下的点?

转载 作者:太空宇宙 更新时间:2023-11-03 20:59:14 26 4
gpt4 key购买 nike

我正在练习 matplotlib.pyplot 并使用屏蔽数据 (np.ma.masked_where) 点。是否有任何数学公式或方法来屏蔽线下方的数据点?预期结果:

最佳答案

是的,检查 y 值是否低于 x 值的线性函数。
在您的情况下,它似乎是第一象限的角平分线,因此偏移量为 0,斜率为 1:

y < x

一般检查

y < m * x + t    # with slope m and offset t

即在你的情况下简单

y.mask = y < x
plt.plot(x, y)
<小时/>

示例:

import numpy as np
import matplotlib.pyplot as plt
plt.style.use('ggplot')

fig = plt.figure()
np.random.seed(7) # prepare data
x = np.random.random(10)
y = np.random.random(10)
y = np.ma.masked_array(y)

# plot all values
plt.plot(x, y, 'o', ms=10, mec='k', mfc=(0,0,0,0), label = 'all points')

y.mask = y < x # mask values below angular bisector
plt.plot(x, y, 'bo', label = '$y \geq x$') # plot masked array
plt.plot((0, 1), (0, 1), 'b') # plot angular bisector


m = 3 # prepare the general case
t = -1
y.mask = y < m * x + t # mask values below linear function
plt.plot(x, y, 'rx', label = '$y \geq 3x - 1$') # plot masked array
plt.plot((0, 1), (m*0+t, m*1+t), 'r') # plot linear function

plt.ylim(0, 1)
fig.legend(ncol=3, loc='upper center')

enter image description here

关于python - 如何屏蔽线以下的点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55810197/

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