散点图上 2 行之间的掩码数据

问题描述

如何屏蔽两行之间的数据?

ax = fig.add_subplot(111)  
ax.scatter(data_x,data_y,s=1,c='grey',alpha=0.3,label='data')
ax.plot(top_line_x,top_line_y,'r--',lw=2,label='top line')
ax.plot(bottom_line_x,bottom_line_y,'b--',label='bottom line')

ax.set_ylim(-6,20)
ax.set_xlim(-1,5)
ax.invert_yaxis()
plt.legend(loc='upper right')

enter image description here

解决方法

我解决了。首先,合并顶行和底行数据(lines_x 和lines_y),然后运行此代码。

from matplotlib.patches import Polygon

poly=Polygon(np.column_stack([lines_x,lines_y]),alpha=0.2,color='yellow')
ax.add_patch(poly)
filtred = poly.contains_points(ax.transData.transform(data))
fdata = df.loc[filtred,:].copy()
fdata.to_csv('filtered_data.csv',index=False)