Matplotlib / wxpython:协调值未显示在状态栏中

时间:2015-04-20 17:56:04

标签: python matplotlib wxpython wxwidgets

我有一个与wxPython集成的简单matplotlib图,由于某种原因,x / y坐标没有显示在状态栏下(移动鼠标时显示的坐标),它们曾经出现过但不能让它再发生。任何想法为什么这可能? (我还尝试手动设置format_coord属性,但似乎无法正常工作)。

我认为wx做了一些有趣的事情,因为如果我只是在一个新的Python shell中创建一个matplotlib图,它可以正常工作。示例代码如下:

#Subclassing WX Panel Class to be able to integrate matplotlib into it
class p1(wx.Panel):
    def __init__(self, parent, frame):

        #Initialize WX Panel
        wx.Panel.__init__(self, parent, -1, size=(50,50))

        #Set up Figure/Canvas
        self.frame = frame
        self.figure = Figure()
        self.canvas = FigureCanvas(self, -1, self.figure)

        #Set up Matplotlib Toolbar
        self.chart_toolbar = NavigationToolbar2Wx(self.canvas)
        tw,th = self.chart_toolbar.GetSizeTuple()
        fw,fh = self.canvas.GetSizeTuple()
        self.chart_toolbar.SetSize(wx.Size(fw, th))
        self.chart_toolbar.Realize()

        graphs_sizer = wx.BoxSizer(wx.VERTICAL)

        graphs_sizer.Add(self.canvas, 20, flag=wx.EXPAND, border=5)
        graphs_sizer.Add(self.chart_toolbar, 1, flag=wx.ALIGN_CENTER, border=5)

        self.SetSizer(graphs_sizer)

    def plot(self):
        self.axs1 = self.figure.add_subplot(1,1,1)
        self.axs1.plot([1,2,3,4,5],color='blue')

class TestFrame(wx.Frame):
    def __init__(self, parent, title):

        #Initialize WX Frame
        wx.Frame.__init__(self, parent, title=title, size=(1000,800))

        #Create Splitter Window and Add Left/Right Panels
        self.splitterWindow = wx.SplitterWindow(self)
        self.panel1 = p1(self.splitterWindow, self)
        self.panel2 = wx.Panel(self.splitterWindow)
        self.splitterWindow.SplitVertically(self.panel1, self.panel2, 700)

        #Create Status Bar
        self.statusbar = self.CreateStatusBar()

        #Plot
        self.panel1.plot()

app = wx.App(redirect=False)
frame = TestFrame(None, "Test")
frame.Show(True)
app.MainLoop()

而像这样简单的东西可以正常工作:

plt.plot([1,2,3,4,5])
plt.show()

1 个答案:

答案 0 :(得分:2)

如果你使代码完整,那么人们可以更容易地提供帮助,例如你的代码就是导入。

请注意,您不需要" SetSize"工具栏的东西,只有在MAC上的一些wxPython版本才需要IIRC。

要获得鼠标坐标,您需要连接一个事件,这可能是用plt.plot开箱即用的。

活动的文档位于:http://matplotlib.org/users/event_handling.html

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

#Subclassing WX Panel Class to be able to integrate matplotlib into it
class p1(wx.Panel):
    def __init__(self, parent, frame):

        #Initialize WX Panel
        wx.Panel.__init__(self, parent, -1, size=(50,50))

        #Set up Figure/Canvas
        self.frame = frame
        self.figure = Figure()
        self.canvas = FigureCanvas(self, -1, self.figure)

        #Set up Matplotlib Toolbar
        self.chart_toolbar = NavigationToolbar2Wx(self.canvas)
        self.chart_toolbar.Realize()

        graphs_sizer = wx.BoxSizer(wx.VERTICAL)

        graphs_sizer.Add(self.canvas, 20, flag=wx.EXPAND, border=5)
        graphs_sizer.Add(self.chart_toolbar, 1, flag=wx.ALIGN_CENTER, border=5)

        self.SetSizer(graphs_sizer)

    def plot(self):
        self.axs1 = self.figure.add_subplot(1,1,1)
        self.axs1.plot([1,2,3,4,5],color='blue')

class TestFrame(wx.Frame):
    def __init__(self, parent, title):

        #Initialize WX Frame
        wx.Frame.__init__(self, parent, title=title, size=(1000,800))

        #Create Splitter Window and Add Left/Right Panels
        self.splitterWindow = wx.SplitterWindow(self)
        self.panel1 = p1(self.splitterWindow, self)
        self.panel2 = wx.Panel(self.splitterWindow)
        self.splitterWindow.SplitVertically(self.panel1, self.panel2, 700)

        #Create Status Bar
        self.statusbar = self.CreateStatusBar()

        #Plot
        self.panel1.plot()

        mouseMoveID = self.panel1.canvas.mpl_connect('motion_notify_event',
                                                     self.onMotion)

    def onMotion(self, evt):
        x = evt.x
        y = evt.y
        inaxes = evt.inaxes
        xdata = evt.xdata
        ydata = evt.ydata
        self.statusbar.SetStatusText("%s, %s, %s, %s, %s" % (
            x, y, inaxes, xdata, ydata))


app = wx.App(redirect=False)
frame = TestFrame(None, "Test")
frame.Show(True)
app.MainLoop()