Radiobutton不起作用

时间:2017-03-16 00:58:22

标签: python python-2.7 python-3.x numpy matplotlib

您好我创建了一个允许绘制多边形的脚本python。实际上我有一些点,我想要的是这样的:我绘制一个多边形,内部的点被删除,其他点被保留。但是我创建了一个RadioButton来做反向,也就是说外面的点被移除而其他点被保留。

这是我的代码,但由于RadioButton

,它无效
import pylab as plt
import numpy as np
from shapely.geometry import Point
from shapely.geometry.polygon import Polygon
from matplotlib.widgets import Button
from matplotlib.widgets import RadioButtons
import os
import Tkinter as tk
from tkFileDialog import askopenfilename
from tkFileDialog import asksaveasfile
import ntpath
from Tkinter import Label
from Tkinter import Listbox
from Tkinter import END
import Tix
import scipy

a = np.array([1,2,3,4,5,6,7,8,9,10])
b = np.sin(a)
c = plt.plot(a,b,'r.')

cd = np.array([])

class Index(object):
    ind = 0



callback = Index()

choice = plt.axes([0.0, 0.92, 0.08, 0.07])
radio = RadioButtons(choice, ('IN', 'OUT'))
def sel(var):
    if var == "IN":

        return True
    elif var == "OUT":

        return False

class Canvas(object):

    def __init__(self,ax):

        self.ax = ax
        self.azer = radio.on_clicked(sel)

        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
        if radio.on_clicked(sel) == True:

            self.vert.append(self.vert[0])
            myList=[]
            for i in range(0,a.size):
                myList.append(Point(float(a[i]),float(b[i])))
                polygon = Polygon(self.vert)
            for i1 in range(0,len(a)):
                if not polygon.contains(myList[i1]):
                    q = i1 
                    a = np.delete(a,q-(len(myList)-len(a)))
                    b = np.delete(b,q-(len(myList)-len(b)))
                    c[0].remove()
                    c = ax.plot(a,b, 'r.')
            for i2 in range(0,len(self.vert)):
                self.vert.pop()
        elif radio.on_clicked(sel) == False:

            self.vert.append(self.vert[0])
            myList=[]
            for i in range(0,a.size):
                myList.append(Point(float(a[i]),float(b[i])))
                polygon = Polygon(self.vert)
            for i1 in range(0,len(a)):
                if polygon.contains(myList[i1]):
                    q = i1 
                    a = np.delete(a,q-(len(myList)-len(a)))
                    b = np.delete(b,q-(len(myList)-len(b)))
                    c[0].remove()
                    c = ax.plot(a,b, 'r.')
            for i2 in range(0,len(self.vert)):
                self.vert.pop()   

    def update_path(self,event):

        if event.inaxes!=self.ax: return 

        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)


plt.show()

0 个答案:

没有答案