2015-02-06 406 views
0

下面是一个代码段我使用在wxPython应用程序来显示2D matplotlib情节:如何使用鼠标在wxPython中旋转matplotlib 3D图?

import matplotlib 
matplotlib.use('WXAgg') 
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg 
from matplotlib.figure import Figure 
import wx 

[wxPython application and class code snipped] 

    figure = Figure() 
    axes = figure.add_subplot(111) 
    canvas = FigureCanvasWxAgg(self, wx.ID_ANY, figure) 
    plotSizer = wx.BoxSizer(wx.VERTICAL) 
    plotSizer.Add(self, canvas, proportion=1, flag=wx.EXPAND) 
    plotPanel = wx.Panel(self, wx.ID_ANY, size=DEFAULT_PLOT_SIZE) 
    plotPanel.SetSizer(plotSizer) 

我可以绘制于轴线,重绘画布和平移和缩放。当我尝试使用3D进行等价处理时,会显示3D图,但我无法旋转/平移/缩放。这段代码的唯一区别是3D的额外导入和add_subplot()的投影参数的添加。

import matplotlib 
matplotlib.use('WXAgg') 
from mpl_toolkits.mplot3d import axes3d 
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg 
from matplotlib.figure import Figure 
import wx 

[wxPython application and class code snipped] 

    figure = Figure() 
    axes = figure.add_subplot(111, projection="3d") 
    canvas = FigureCanvasWxAgg(self, wx.ID_ANY, figure) 
    plotSizer = wx.BoxSizer(wx.VERTICAL) 
    plotSizer.Add(self, canvas, proportion=1, flag=wx.EXPAND) 
    plotPanel = wx.Panel(self, wx.ID_ANY, size=DEFAULT_PLOT_SIZE) 
    plotPanel.SetSizer(plotSizer) 

我得到这样的警告:

...\site-packages\mpl_toolkits\mplot3d\axes3d.py:1009: UserWarning: Axes3D.figure.canvas is 'None', mouse rotation disabled. Set canvas then call Axes3D.mouse_init(). 

所以,我试图调用FigureCanvasWxAgg()之后,此代码设置Axes3D.figure.canvas:

axes.figure.canvas = canvas 
axes.mouse_init() 

但是这并未”工作;我仍然无法使用鼠标旋转3D图。

演示源代码http://matplotlib.org/mpl_toolkits/mplot3d/tutorial.html,使用独立的matplotlib工作;我可以用鼠标在那里旋转地块。如何在使用wxPython时使鼠标旋转工作?

+0

你可以给一个小的runnable样本? – Werner 2015-02-08 10:11:23

回答

0

原来我只是交换了画布和轴创建的顺序。应该先创建画布并将其添加到图形中,然后可以创建3D轴。

figure = Figure() 
canvas = FigureCanvasWxAgg(self, wx.ID_ANY, figure) 
axes = figure.add_subplot(111, projection="3d") 
plotSizer = wx.BoxSizer(wx.VERTICAL) 
plotSizer.Add(self, canvas, proportion=1, flag=wx.EXPAND) 
plotPanel = wx.Panel(self, wx.ID_ANY, size=DEFAULT_PLOT_SIZE) 
plotPanel.SetSizer(plotSizer)