1

I'm using the approach shown in the following questions for embedding an Axes3D in a PyQt4 widget:

Qt4 + mplot3d of matplotlib

When starting the application, I can not rotate the axes via mouse movement anymore. What do I need to do to enable the rotation again?

Community
  • 1
  • 1
Christian
  • 2,214
  • 4
  • 26
  • 37

4 Answers4

2

Any time you call something like Axes3D.clear(), then to enable mouse rotation again you have to call Axes3D.mouse_init().

Seems to be undocumented, but it works for me! Source

Cuadue
  • 3,769
  • 3
  • 24
  • 38
0

I had a similar problem, but my axes would not appear. I called FigureCanvas.__init__() then added the axes to the figure by calling self.ax = self.fig.add_subplot(111, projection='3d'), as recommended by Björn

Community
  • 1
  • 1
crouserj
  • 13
  • 5
0

Here, in Python 3, after setting global variable self.ax I switched mouse rotating on by calling self.ax.mouse_init(). Perhaps it helps you.

self.fig = Figure(figsize = (5,5), dpi = 100)
self.ax = self.fig.add_subplot(1,1,1, projection='3d')
self.canvas = FigureCanvasTkAgg(self.fig, root)
self.ax.mouse_init()
self.canvas.draw()
self.canvas.get_tk_widget().grid()
  • Welcome to Stack Overflow! Generally, answers are much more helpful if they include an explanation of what the code is intended to do, and why that solves the problem without introducing others. – Tim Diekmann May 21 '18 at 11:49
0

I had the same problem and solved it after deep debugging. The problem is an inconsistency of the canvas instances. The example code first constructs a figure which automatically provides the figure with a canvas instance. Then an axes will be added to the figure and then the figure's canvas replaced by another canvas which can be used as QWidget.

But, an Axes3D constructor adds callback functions to its figure's canvas to implement the rotation feature. So, if the canvas is replaced after that, the connection to the rotation functions gets lost.

Maybe it's enough to simply call FigureCanvas.__init__() before adding the axes to the figure, but I haven't tried that.

My working solution is a bit different: First I call matplotlib.use("Qt4Agg") to set the correct backend. Next, I construct figures with fig = matplotlib.pyplot.figure() which makes sure they already contain the right canvas and then add the Axes3D to them. I access the canvas simply as data element fig.canvas and use it as QWidget. It is also important to call QtGui.QApplication() before constructing any figure.

Björn
  • 1
  • 1
  • 1
    I think I'm actually doing exaclty that, but it doesn't work. Maybe you can have a look at my code (46 lines) at http://pastebin.com/Y7Fcsnpp and might have an idea on what's going wrong. – Christian Jan 31 '12 at 10:26