Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用matplotlib';wxPython内的事件处理?_Python_Matplotlib_Wxpython - Fatal编程技术网

如何使用matplotlib';wxPython内的事件处理?

如何使用matplotlib';wxPython内的事件处理?,python,matplotlib,wxpython,Python,Matplotlib,Wxpython,我试图修改matplotlib文档中找到的,以便它在wxPython窗口中工作 到目前为止,我有以下几点: import wx import matplotlib matplotlib.interactive(True) matplotlib.use('WXAgg') from matplotlib.figure import Figure from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanv

我试图修改matplotlib文档中找到的,以便它在wxPython窗口中工作

到目前为止,我有以下几点:

import wx

import matplotlib
matplotlib.interactive(True)
matplotlib.use('WXAgg')
from matplotlib.figure import Figure
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas

import numpy as np

class DraggableRectangle:
    def __init__(self, rect):
        self.rect = rect
        self.press = None

    def connect(self):
        'connect to all the events we need'
        self.cidpress = self.rect.figure.canvas.mpl_connect(
            'button_press_event', self.on_press)
        self.cidrelease = self.rect.figure.canvas.mpl_connect(
            'button_release_event', self.on_release)
        self.cidmotion = self.rect.figure.canvas.mpl_connect(
            'motion_notify_event', self.on_motion)

    def on_press(self, event):
        'on button press we will see if the mouse is over us and store some data'
        if event.inaxes != self.rect.axes: return

        contains, attrd = self.rect.contains(event)
        if not contains: return
        print 'event contains', self.rect.xy
        x0, y0 = self.rect.xy
        self.press = x0, y0, event.xdata, event.ydata

    def on_motion(self, event):
        'on motion we will move the rect if the mouse is over us'
        if self.press is None: return
        if event.inaxes != self.rect.axes: return
        x0, y0, xpress, ypress = self.press
        dx = event.xdata - xpress
        dy = event.ydata - ypress
        #print 'x0=%f, xpress=%f, event.xdata=%f, dx=%f, x0+dx=%f'%(x0, xpress, event.xdata, dx, x0+dx)
        self.rect.set_x(x0+dx)
        self.rect.set_y(y0+dy)

        #self.rect.figure.canvas.draw()
        self.rect.figure.canvas.draw_idle()

    def on_release(self, event):
        'on release we reset the press data'
        self.press = None
        #self.rect.figure.canvas.draw()
        self.rect.figure.canvas.draw_idle()

    def disconnect(self):
        'disconnect all the stored connection ids'
        self.rect.figure.canvas.mpl_disconnect(self.cidpress)
        self.rect.figure.canvas.mpl_disconnect(self.cidrelease)
        self.rect.figure.canvas.mpl_disconnect(self.cidmotion)

class Frame(wx.Frame):
    def __init__(self, title):
        wx.Frame.__init__(self, None, title=title, pos=(150,150), size=(800,600))

        self.panel = wx.Panel(self)

        self.figure = Figure(figsize=(6, 4), dpi=100)
        self.axes = self.figure.add_subplot(111)
        self.canvas = FigureCanvas(self.panel, wx.ID_ANY, self.figure)

        rects = self.axes.bar(range(10), 20*np.random.rand(10))
        drs = []
        for rect in rects:
            dr = DraggableRectangle(rect)
            dr.connect()
            drs.append(dr)

app = wx.App(redirect=False)
top = Frame("test")
top.Show()
app.MainLoop()

不幸的是,这并不像广告宣传的那样有效;条形图显示在图表上,但它们根本不可拖动。按原样复制和粘贴示例效果很好,只有当我尝试将其与wxPython一起使用时,我才会遇到问题。

不要问我为什么,但将
drs
更改为
self.drs
(第75行和第79行)似乎有效。好像你必须阻止
drs
和所有
dr
被垃圾收集。(与什么有关?)


你能告诉我你的回调是否启动了吗?我可以用print语句告诉你——它们没有启动。你能添加工具栏,看看缩放和平移是否正常工作吗?我正在试图找出是否所有回调都已中断,或者只是您的新回调未连接。
import wx

import matplotlib
matplotlib.interactive(True)
matplotlib.use('WXAgg')
from matplotlib.figure import Figure
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas

import numpy as np

class DraggableRectangle:
    def __init__(self, rect):
        self.rect = rect
        self.press = None

    def connect(self):
        'connect to all the events we need'
        self.cidpress = self.rect.figure.canvas.mpl_connect(
            'button_press_event', self.on_press)
        self.cidrelease = self.rect.figure.canvas.mpl_connect(
            'button_release_event', self.on_release)
        self.cidmotion = self.rect.figure.canvas.mpl_connect(
            'motion_notify_event', self.on_motion)

    def on_press(self, event):
        print "on_press"
        'on button press we will see if the mouse is over us and store some data'
        if event.inaxes != self.rect.axes: return

        contains, attrd = self.rect.contains(event)
        if not contains: return
        print 'event contains', self.rect.xy
        x0, y0 = self.rect.xy
        self.press = x0, y0, event.xdata, event.ydata

    def on_motion(self, event):
        print "on_motion"
        'on motion we will move the rect if the mouse is over us'
        if self.press is None: return
        if event.inaxes != self.rect.axes: return
        x0, y0, xpress, ypress = self.press
        dx = event.xdata - xpress
        dy = event.ydata - ypress
        #print 'x0=%f, xpress=%f, event.xdata=%f, dx=%f, x0+dx=%f'%(x0, xpress, event.xdata, dx, x0+dx)
        self.rect.set_x(x0+dx)
        self.rect.set_y(y0+dy)

        #self.rect.figure.canvas.draw()
        self.rect.figure.canvas.draw_idle()

    def on_release(self, event):
        print "on_release"
        'on release we reset the press data'
        self.press = None
        #self.rect.figure.canvas.draw()
        self.rect.figure.canvas.draw_idle()

    def disconnect(self):
        'disconnect all the stored connection ids'
        self.rect.figure.canvas.mpl_disconnect(self.cidpress)
        self.rect.figure.canvas.mpl_disconnect(self.cidrelease)
        self.rect.figure.canvas.mpl_disconnect(self.cidmotion)

class Frame(wx.Frame):
    def __init__(self, title):
        wx.Frame.__init__(self, None, title=title, pos=(150,150), size=(800,600))

        self.panel = wx.Panel(self)

        self.figure = Figure(figsize=(6, 4), dpi=100)
        self.axes = self.figure.add_subplot(111)
        self.canvas = FigureCanvas(self.panel, wx.ID_ANY, self.figure)

        rects = self.axes.bar(range(10), 20*np.random.rand(10))
        self.drs = []
        for rect in rects:
            dr = DraggableRectangle(rect)
            dr.connect()
            self.drs.append(dr)

print "test"
app = wx.PySimpleApp()#redirect=False)
top = Frame("test")
top.Show()
app.MainLoop()