views:

82

answers:

1

I'm writing an application in wxPython. The button "Plot" shows the wxFrame with 3D surface but I can't rotate the surface. Is it possible to rotate a 3D plot placed in a wxFrame? How can I do?

Example:

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

import wx

from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt

class CanvasFrame(wx.Frame):

    def __init__(self):
        wx.Frame.__init__(self,  None, -1, 'CanvasFrame', size=(550,350))

        self.SetBackgroundColour(wx.NamedColor("WHITE"))

        self.figure = plt.figure()
        self.axes = axes3d.Axes3D(self.figure)
        X, Y, Z = axes3d.get_test_data(0.05)
        self.axes.plot_surface(X, Y, Z, rstride=8, cstride=8, alpha=0.3)
        cset = self.axes.contour(X, Y, Z, zdir='z', offset=-100)
        cset = self.axes.contour(X, Y, Z, zdir='x', offset=-40)
        cset = self.axes.contour(X, Y, Z, zdir='y', offset=40)

        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.Fit()

        self.add_toolbar()

    def add_toolbar(self):
        self.toolbar = NavigationToolbar2Wx(self.canvas)
        self.toolbar.Realize()
        if wx.Platform == '__WXMAC__':
            self.SetToolBar(self.toolbar)
        else:
            tw, th = self.toolbar.GetSizeTuple()
            fw, fh = self.canvas.GetSizeTuple()
            self.toolbar.SetSize(wx.Size(fw, th))
            self.sizer.Add(self.toolbar, 0, wx.LEFT | wx.EXPAND)
        self.toolbar.update()

    def OnPaint(self, event):
        self.canvas.draw()

class App(wx.App):

    def OnInit(self):
        frame = CanvasFrame()
        frame.Show(True)

        return True

app = App(0)
app.MainLoop()
+2  A: 

You'd probably be better off asking on the wxPython google group since I've seen people on there using matplotlib. Anyway, looking at the matplotlib website, it appears that there is a wxMpl module that might help: http://agni.phys.iit.edu/~kmcivor/wxmpl/

My guess is, if matplotlib can rotate the plot outside of wx, then you just need to pass some events from wx to matplotlib to make it work within wx.

Mike Driscoll