隐藏散点图中绘制线上方的所有点

2024-04-20 05:06:48 发布

您现在位置:Python中文网/ 问答频道 /正文

绘制两个星系等效宽度比的列表,我将它们相对绘制,然后我想消除线y=(0.61)/(x-0.05)+1.3上方的所有点,这样只有下面的点保留在绘图中。这里是我到目前为止,我已经看了堆栈溢出,但对如何做到这一点迷失了方向。注意:我只希望上面的点y而不是y2被消除,谢谢!你知道吗

plt.scatter(NT,OT,s=5, c='r', marker='o')
x = np.linspace(-5,5,100)
y=(0.61)/(x-0.05)+1.3
y2 =((0.61)/(x-0.47)) + 1.19#
plt.plot(x, y, '-k')
plt.plot(x, y2, '--k')
plt.xlabel('log(NII/HA)', color='#1C2833')
plt.ylabel('log(OIII/HB)', color='#1C2833')
plt.ylim((-1,1.5))   
plt.xlim((-2,0.5))  

提前感谢,如有任何建议将不胜感激!你知道吗


Tags: log绘图列表宽度plot堆栈绘制plt
1条回答
网友
1楼 · 发布于 2024-04-20 05:06:48

我相信最简单的方法是在绘图前过滤出所需的点,你可以用

filteredx = list()
filteredy = list()
for ii in range(len(NT)):
    if OT[ii] < (0.61) / (NT[ii] - 0.05) + 1.3:    #Adjust equation as needed
        filteredx.append(NT[ii])
        filteredy.append(OT[ii])
plt.scatter(filteredx, filteredy)

完整示例:

import numpy as np
import matplotlib.pyplot as plt

# Create random data for demonstration
np.random.seed(1)
n = 200
datay = np.random.randn(n)*50
datax = np.random.randn(n)*1.5
x = np.linspace(-5, 5, 1000)
y = (0.61) / (x - 0.05) + 1.3

filteredx = list()
filteredy = list()
plt.scatter(datax, datay, marker='o', color='g', label="All Points")
for ii in range(n):
    if datay[ii] < (0.61) / (datax[ii] - 0.05) + 1.3:
        filteredx.append(datax[ii])
        filteredy.append(datay[ii])
plt.scatter(filteredx, filteredy, marker='.', color='r', label="After Filtering")
plt.plot(x,y, linewidth=1, linestyle='-')
plt.legend()
plt.show()

Filtering demonstration

要删除渐近线,可以过滤大于或小于的值 x = 0.05并分别绘制两组,即

plt.plot(x[x < 0.05], y[x < 0.05], linewidth=1, linestyle='-', color='b')
plt.plot(x[x > 0.05], y[x > 0.05], linewidth=1, linestyle='-', color='b')

Filtering below line

在这种情况下,您确实需要指定颜色,否则这两个区域将具有不同的颜色。此外,如果您需要为图例标记行,只需在两个区域中指定一个a label——如果您为每个区域指定一个,则最终将获得两个图例条目。你知道吗

相关问题 更多 >