您好我想用Python做一个脚本。实际上我想打开一个包含数据的文件,然后选择创建多边形的数据来删除多边形内的数据。我有两个代码。第一个允许打开文件并显示数据:
import numpy as np
import matplotlib.pyplot as plt
import pylab as plt
import os
from matplotlib.widgets import Button
import Tkinter as tk
from tkFileDialog import askopenfilename
abscissa = []
ordinate = []
figure, ax1 = plt.subplots()
class Index(object):
ind = 0
def pre(self, event):
root = tk.Tk()
root.withdraw()
file_name = askopenfilename(title = 'Open file', filetypes = [('All','*'),('PGas data', 'pgas')])
print(file_name)
array = []
arrays = []
indice = []
with open(file_name, 'r') as ins:
for line in ins:
array.append(str(line))
for i in range(0,len(array)-1):
if 'abscissa = [' in array[i]:
indice.append(i)
elif ']' in array[i]:
indice.append(i)
elif 'ordinate = [' in array[i]:
indice.append(i)
i1 = indice[0]+1
while i1 < indice[1]:
abscissa.append(array[i1])
i1 = i1 + 1
i2 = indice[2]+1
while i2 < indice[3]:
ordinate.append(array[i2])
i2 = i2 + 1
ax1.plot(abscissa, ordinate,'o')
plt.draw()
def nex(self, event):
a = np.array([1,2,3,4,5,6])
b = np.array([7,5,6,2,7,1])
q = ax1.plot(a,b,'.')
plt.draw()
def log(self,event):
ax1.set_xscale('log', basex=10)
ax1.set_yscale('log', basex=10)
plt.draw()
def linear(self,event):
ax1.set_xscale('linear')
ax1.set_yscale('linear')
plt.draw()
callback = Index()
axpre = plt.axes([0.0, 0.92, 0.08, 0.07])
axnex = plt.axes([0.11, 0.92, 0.08, 0.07])
axlog = plt.axes([0.22, 0.92, 0.08, 0.07])
axlinear = plt.axes([0.33, 0.92, 0.08, 0.07])
bnex = Button(axnex, 'Save')
bnex.on_clicked(callback.nex)
bpre = Button(axpre, 'Open')
bpre.on_clicked(callback.pre)
blog = Button(axlog, 'Log')
blog.on_clicked(callback.log)
blinear = Button(axlinear, 'Linear')
blinear.on_clicked(callback.linear)
plt.show()
第二个允许选择这样的数据我单击左键来执行多边形I clik在右键上关闭多边形,内部数据将被删除。但是我没有实现融合这两个代码。也就是说我想加载数据然后创建一个允许删除多边形内部数据的多边形。这是我的代码:
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,4,5,6,7,8,9,50,500])
b = np.array([1,4,5,5,4,5,4,5,9,25,800])
c = plt.plot(a,b, '.')
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.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):
global a,b,c
self.vert.append(self.vert[0])
myList=[]
for i in range(0,a.size):
myList.append(Point(a[i],b[i]))
polygon = Polygon(self.vert)
for i1 in range(0,len(a)):
if polygon.contains(myList[i1]):
#global a,b
q = i1
a = np.delete(a,q-(len(myList)-len(a)))
b = np.delete(b,q-(len(myList)-len(b)))
#q = q-1
c[0].remove()
#global c
c = plt.plot(a,b, 'b.')
for i2 in range(0,len(self.vert)):
self.vert.pop()
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()
非常感谢你的帮助!!!