如何掩盖线下的点?

时间:2019-04-23 11:29:25

标签: python python-3.x matplotlib

我正在使用matplotlib.pyplot进行练习,并开始使用掩蔽数据(np.ma.masked_where)点。有什么数学公式或方法可以掩盖线下的数据点?预期结果:

2 个答案:

答案 0 :(得分:0)

points_X = [1,2,3,4,5,6,7,8,9,10] // your points_X data
points_Y = [1,2,3,4,5,6,7,8,9,10] // your points_Y data

new_points_X=[]
new_points_Y=[]

for i in range(len(points_X)):
    if(points_Y[i] <= points_X[i]):
        new_points_Y.append(points_Y[i])
        new_points_X.append(points_X[i])

plot(new_points_X, new_points_Y)  

答案 1 :(得分:0)

是的,请检查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