我在wx.Frame(或wx.Panel)的Matplotlib图画布中间歇性地使用Matplotlib NavigationToolbar2Wx时遇到过这种行为。如果选择了缩放图标或平移图标,则图标会消失,但空白区域中的单击仍会切换工具。 Home,Backward step或Forward步骤的图标都按预期运行。
任何人都可以提供以下建议:1。导致它的原因以及2.如何解决?
感谢joaquin发布略微修改的初始代码以包含工具栏。 (http://stackoverflow.com/questions/10737459/embedding-a-matplotlib-figure-inside-a-wxpython-panel)
我使用python 2.6,wxPython 2.9.2.4 osx-carbon(经典)和Matplotlib 1.1.0
由于
下面的代码显示了问题:
#!/usr/bin/env python
# encoding: UTF-8
"""
wxPython and Matplotlib Canvas with Matplotlib Toolbar.py
"""
from numpy import arange, sin, pi
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
import wx
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)
# Add Matplotlib Toolbar
# Add the Matplotlib Navigation toolBar here
self.toolbar=NavigationToolbar2Wx(self.canvas)
self.toolbar.AddLabelTool(5,'',wx.ArtProvider.GetBitmap(wx.ART_FILE_OPEN, wx.ART_TOOLBAR, (32,32)))
#self.Bind(wx.EVT_TOOL, self.NewTitle(), id=5)
self.toolbar.Realize()
# Add to Box Sizer
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.Add(self.toolbar, 0, wx.LEFT | wx.TOP | wx.GROW)
self.sizer.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW)
self.SetSizer(self.sizer)
self.Fit()
def draw(self):
t = arange(0.0, 3.0, 0.01)
s = sin(2 * pi * t)
self.axes.plot(t, s)
if __name__ == "__main__":
app = wx.PySimpleApp()
fr = wx.Frame(None, title='test',size=(800,600))
panel = CanvasPanel(fr)
panel.draw()
fr.Show()
app.MainLoop()
答案 0 :(得分:0)
我不能评论这个具体问题的原因, 但是我在使用wxpython 2.9的非Agg后端遇到了一些问题(虽然代码在2.8中运行正常)。用Agg版替换工具栏为我解决了这些问题; e.g:
from matplotlib.backends.backend_wx import NavigationToolbar2Wx
==>
from matplotlib.backends.backend_wxagg import NavigationToolbar2WxAgg
并相应调整代码:
self.toolbar=NavigationToolbar2Wx(self.canvas)
==>
self.toolbar = NavigationToolbar2WxAgg(self.canvas)
HTH, VBR