Skip to main content
 首页 » 编程设计

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

2024年11月24日21飞鱼

绘制 2 个星系等效宽度比列表,我将它们相互绘制,然后我想消除线 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))   

在此先感谢任何建议!

请您参考如下方法:

我相信最简单的方法是在绘图之前简单地过滤出所需的点,您可以这样做

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

要删除渐近线,您可以过滤大于和小于的值 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') 

在这种情况下你确实需要指定颜色,否则两个区域会有不同的颜色。此外,如果您需要为图例标记线,只需给两者之一一个 label - 如果您给每个区域一个,您最终会得到两个图例条目。