Python Matplotlib拖放文件

Python Matplotlib拖放文件,python,matplotlib,Python,Matplotlib,我希望能够在Matplotlib绘图上拖放一个文件,并对该文件执行一些操作(例如打开并绘图!)。不幸的是,虽然我的脚本使用以下方法处理一些事件: fig.canvas.mpl\u connect('button\u release\u event',self.btn\u release) 诸如此类,我找不到任何能返回文件路径的东西 我还没有达到创建带有嵌入式绘图的GUI的水平,所以如果有一个不这样做的解决方案,我会更喜欢它 您似乎希望由mpl\u connect处理drop\u file\u事件

我希望能够在Matplotlib绘图上拖放一个文件,并对该文件执行一些操作(例如打开并绘图!)。不幸的是,虽然我的脚本使用以下方法处理一些事件:
fig.canvas.mpl\u connect('button\u release\u event',self.btn\u release)
诸如此类,我找不到任何能返回文件路径的东西


我还没有达到创建带有嵌入式绘图的GUI的水平,所以如果有一个不这样做的解决方案,我会更喜欢它

您似乎希望由
mpl\u connect
处理
drop\u file\u事件
;不幸的是,根据的情况并非如此(对于通才绘图库来说,这种情况有点特殊)

然而,实现自己的GUI处理drop事件并不是那么困难。下面的示例主要基于并添加了一个
wx.FileDropTarget

# Used to guarantee to use at least Wx2.8
import wxversion
wxversion.ensureMinimal('2.8')

import numpy as np
import wx

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


class MyFileDropTarget(wx.FileDropTarget):
    def __init__(self, window):
        wx.FileDropTarget.__init__(self)
        self.window = window

    def OnDropFiles(self, x, y, filenames):
        fig = self.window.figure
        inaxes = fig.get_axes()[0]
        h_pix = int(fig.get_figheight() * fig.get_dpi()) # fig height in pixels
        message = "%d file(s) dropped at (%d,%d):\n" % (len(filenames), x, y)
        for file in filenames:
            message += file + "\n"
        inaxes.annotate(message, (x, h_pix-y), xycoords='figure pixels')        
        self.window.draw()

class CanvasPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        self.figure = Figure()
        self.axes = self.figure.add_subplot(111)
        self.canvas = FigureCanvas(self, -1, self.figure)
        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.sizer.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW)
        self.SetSizer(self.sizer)
        self.add_toolbar()
        self.Fit()

        win_target = self.canvas
        dt = MyFileDropTarget(win_target)
        win_target.SetDropTarget(dt)

    def draw(self):
        t = np.linspace(0.0, 2., 100)
        s = np.sin(2 * np.pi * t)
        self.axes.plot(t, s)

    def add_toolbar(self):
        self.toolbar = NavigationToolbar2Wx(self.canvas)
        self.toolbar.Realize()
        if wx.Platform == '__WXMAC__':
            # Mac platform (OSX 10.3, MacPython) does not seem to cope with
            # having a toolbar in a sizer. This work-around gets the buttons
            # back, but at the expense of having the toolbar at the top
            self.SetToolBar(self.toolbar)
        else:
            # On Windows platform, default window size is incorrect, so set
            # toolbar width to figure width.
            tw, th = self.toolbar.GetSizeTuple()
            fw, fh = self.canvas.GetSizeTuple()
            # By adding toolbar in sizer, we are able to put it at the bottom
            # of the frame - so appearance is closer to GTK version.
            # As noted above, doesn't work for Mac.
            self.toolbar.SetSize(wx.Size(fw, th))
            self.sizer.Add(self.toolbar, 0, wx.LEFT | wx.EXPAND)
        # update the axes menu on the toolbar
        self.toolbar.update()

if __name__ == "__main__":
    app = wx.PySimpleApp()
    frame = wx.Frame(None, title='File drop test')
    panel = CanvasPanel(frame)
    panel.draw()
    frame.Show()
    app.MainLoop()

很高兴知道我没有错过什么。我进入GUI的冒险比我计划的要早。谢谢你的例子。