How to enable rotation in an Axes3D (matplotlib) e

2019-05-30 08:46发布

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?

4条回答
等我变得足够好
2楼-- · 2019-05-30 08:51

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

查看更多
【Aperson】
3楼-- · 2019-05-30 08:52

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()
查看更多
可以哭但决不认输i
4楼-- · 2019-05-30 08:53

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

查看更多
混吃等死
5楼-- · 2019-05-30 09:10

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.

查看更多
登录 后发表回答