是否可以使用python在绘图中选择数据并删除所选数据?
你好,我做了一个python文件,通过创建一个多边形,可以选择数据,然后我想删除多边形内的点。这是我的代码:是否可以使用python在绘图中选择数据并删除所选数据?,python,python-2.7,python-3.x,numpy,matplotlib,Python,Python 2.7,Python 3.x,Numpy,Matplotlib,你好,我做了一个python文件,通过创建一个多边形,可以选择数据,然后我想删除多边形内的点。这是我的代码: import pylab as plt import numpy as np from shapely.geometry import Point from shapely.geometry.polygon import Polygon #from matplotlib.widgets import Button a = np.array([1,2,3,5,7]) b = np.arr
import pylab as plt
import numpy as np
from shapely.geometry import Point
from shapely.geometry.polygon import Polygon
#from matplotlib.widgets import Button
a = np.array([1,2,3,5,7])
b = np.array([1,6,5,7,9])
plt.plot(a,b, '.')
myList=[]
class Canvas(object):
def __init__(self,ax):
self.ax = ax
# Create handle for a path of connected points
self.path, = ax.plot([],[],'o-',lw=3)
self.vert = []
self.ax.set_title('LEFT: new point, MIDDLE: delete last point, RIGHT: close polygon')
self.x = []
self.y = []
self.mouse_button = {1: self._add_point, 2: self._delete_point, 3: self._close_polygon}
def set_location(self,event):
if event.inaxes:
self.x = event.xdata
self.y = event.ydata
def _add_point(self):
self.vert.append((self.x,self.y))
def _delete_point(self):
if len(self.vert)>0:
self.vert.pop()
def _close_polygon(self):
self.vert.append(self.vert[0])
for i in range(0,a.size):
myList.append(Point(a[i],b[i]))
polygon = Polygon(self.vert)
for i1 in range(0,a.size):
if polygon.contains(myList[i1]):
global a,b
a = np.delete(a,i1)
b = np.delete(b,i1)
plt.plot(a,b, 'g.')
def update_path(self,event):
# If the mouse pointer is not on the canvas, ignore buttons
if not event.inaxes: return
# Do whichever action correspond to the mouse button clicked
self.mouse_button[event.button]()
x = [self.vert[k][0] for k in range(len(self.vert))]
y = [self.vert[k][1] for k in range(len(self.vert))]
self.path.set_data(x,y)
plt.draw()
if __name__ == '__main__':
fig = plt.figure(1,(8,8))
ax = fig.add_subplot(111)
cnv = Canvas(ax)
plt.connect('button_press_event',cnv.update_path)
plt.connect('motion_notify_event',cnv.set_location)
class Index(object):
ind = 0
def next(self, event):
plt.draw()
def prev(self, event):
plt.draw()
callback = Index()
plt.show()
但我有两个问题:首先,我不知道为什么,但多边形内的点留在图形中。然后,多边形内的点变成绿色,而它们不在多边形外。非常感谢你的帮助 也许会有帮助?谢谢,但我现在想要的是能够删除这一点:)