Axes3d.mouse_init(), facing problems when embedding matplotlib 3d-projection into PyQt5 App

Christian (again, keeping things on-list),

The entire point of the example was to provide a proof of concept and to show the absolute minimum needed to get mplot3d embedded. Please do not disregard it because I did not use Qt Designer. If I understand Qt Designer correctly, it merely provides additional widgets to be included in the app, so the figure really doesn’t care about that stuff and should be completely orthogonal to our problem.

When you run the example I gave you, it works without any need to call mouse_init(). The fact that it works without any call to mouse_init() proves that something is going very wrong in your code. The only reason for it to be called by the user is if the user managed to disable the user interactions and needs to restore it. That should never happen unless the user explicitly makes that happen.

What I see in your code is you creating the axes before creating the figure canvas. Look at my example closely. The order of the commands matter. If you are creating axes objects in a figure, and then adding a canvas, you are overwriting the auto-generated canvas that was implicitly created when you added axes objects. That is why you are losing interactivity, because all of the original callbacks were being attached to the original canvas that you overwrote. This was also noted in the SO link that Thomas gave you.

I hope that clarifies things for you,

Ben Root

···

On Wed, Apr 29, 2015 at 3:12 PM, Christian Ambros <ambrosc@…2693…> wrote:

Nice, but not close to the task on hand, because no Qt Designer was used, so no gui-elements from there, no embedding! And that was the point to all this.

But maybe I’v come up with a solution to my problem…
Your remember the lines:

*def main(argv):
app = QtWidgets.QApplication(argv)

mainwindow = Main()
mainwindow.show()

fig1 = Figure()
#ax1f1 = fig1.add_subplot(111)

fname='augtr_16MnCrS5.dat'
arr = np.genfromtxt(fname, delimiter=' ', usecols=(0,4,25), missing_values={0:' '}, filling_values={0:0}, unpack=True)
#ax1f1.plot(np.random.rand(20))
#ax1f1.plot(arr(0,25))
#fig = plt.figure()
ax1f1 = fig1.add_subplot(111, projection='3d')
Y, Z, X = arr
ax1f1.plot_wireframe(X, Y, Z, rstride=10, cstride=10)
#plt.show()

fig2 = Figure()
ax1f2 = fig2.add_subplot(211)
ax1f2.plot(Y, Z) #np.random.rand(10)
ax2f2 = fig2.add_subplot(212)
ax2f2.plot(Y, X)#np.random.rand(10)

fig3 = Figure()
ax1f3 = fig3.add_subplot(111)
ax1f3.pcolormesh(np.random.rand(20,20))

mainwindow.addmpl(fig1)
mainwindow.addfig('figure 1', fig1)
mainwindow.addfig('figure 2', fig2)
mainwindow.addfig('figure 3', fig3)

#input()
#mainwindow.rmmpl()
#mainwindow.addmpl(fig2)
#mainwindow.addfig('figure 2', fig2)
sys.exit(app.exec_())*

Let’s take a look at the important part:

*def main(argv):
app = QtWidgets.QApplication(argv)

mainwindow = Main()
mainwindow.show()

fig1 = Figure()
#ax1f1 = fig1.add_subplot(111)

fname='augtr_16MnCrS5.dat'
arr = np.genfromtxt(fname, delimiter=' ', usecols=(0,4,25), missing_values={0:' '}, filling_values={0:0}, unpack=True)
#ax1f1.plot(np.random.rand(20))
#ax1f1.plot(arr(0,25))
#fig = plt.figure()
ax1f1 = fig1.add_subplot(111, projection='3d')
Y, Z, X = arr
ax1f1.plot_wireframe(X, Y, Z, rstride=10, cstride=10)
#plt.show()

fig2 = Figure()
ax1f2 = fig2.add_subplot(211)
ax1f2.plot(Y, Z) #np.random.rand(10)
ax2f2 = fig2.add_subplot(212)
ax2f2.plot(Y, X)#np.random.rand(10)

fig3 = Figure()
ax1f3 = fig3.add_subplot(111)
ax1f3.pcolormesh(np.random.rand(20,20))

mainwindow.addmpl(fig1)
mainwindow.addfig('figure 1', fig1)
mainwindow.addfig('figure 2', fig2)
mainwindow.addfig('figure 3', fig3)

#input()
#mainwindow.rmmpl()
#mainwindow.addmpl(fig2)
#mainwindow.addfig('figure 2', fig2)
sys.exit(app.exec_())*

… and skip the rest…

if I just initialize the mouse upon the axis like this:

fig1 = Figure()
fname='augtr_16MnCrS5.dat'
arr = np.genfromtxt(fname, delimiter=' ', usecols=(0,4,25), missing_values={0:' '}, filling_values={0:0}, unpack=True)
ax1f1 = fig1.add_subplot(111, projection='3d')
Y, Z, X = arr

mainwindow.addmpl(fig1)
ax1f1.mouse_init()

ax1f1.plot_wireframe(X, Y, Z, rstride=10, cstride=10)

than mouse rotation is back online, no warning, no error.

As I asked at the beginning, it was just the right place where the mouse_init() has to be put.

It bothered me so much that I dug into API again and searched for mouse_init(). Taking the error message into account, “mouse rotation off” I figured out that it’s an axis problem. It’s not the canvas which is rotated, it’s all about the axis, so I found the usage for the mouse_init().

Thank you very much for all your effort and the patience to go this way with me.

At last I have a good advice for you: Go and switch to using Qt Designer and PyQt5, it really helps having good gui’s for the uninitiated.

cheers,

Christian

Ps: I’ll get back to you, when I’m done reading your book completely.


“A little learning never caused anyone’s head to explode!”

“Ein wenig Lernen hat noch niemandens Kopf zum Explodieren gebracht!”

On Wednesday, April 29, 2015 1:46 PM, Benjamin Root <ben.root@…1304…> wrote:

Here is a proof of concept (yes, it uses qt4… my work computer doesn’t have qt5, but that should be a straight-forward modification to make). Note the complete lack of any call to mouse_init() and the complete lack of any use of pyplot (in fact, I commented it out to make the point that you shouldn’t use pyplot at all when doing this sort of embedding).

import numpy as np
#import matplotlib.pyplot as plt
import sys
from matplotlib.backends.qt4_compat import QtGui, QtCore
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas

from mpl_toolkits.mplot3d import Axes3D


if __name__ == '__main__':
    # Must come before any Qt widgets are made
    app = QtGui.QApplication(sys.argv)
    win = QtGui.QMainWindow()
    fig = Figure()
    canvas = FigureCanvas(fig)
    ax = fig.add_subplot(1, 1, 1, projection='3d')

    xs = np.random.rand(25)
    ys = np.random.rand(25)
    zs = np.random.rand(25)
    ax.scatter(xs, ys, zs)

    win.resize(int(fig.bbox.width), int(fig.bbox.height))
    win.setWindowTitle("Embedding with Qt")
    # Needed for keyboard events
    canvas.setFocusPolicy(QtCore.Qt.StrongFocus)
    canvas.setFocus()
    win.setCentralWidget(canvas)
    win.show()
    sys.exit(app.exec_())

I hope this helps!

Ben Root

On Wed, Apr 29, 2015 at 5:38 AM, Christian Ambros <ambrosc@…2693…> wrote:

Ok, back from revision…

The is no mix-up for the show command. The only explicit show() command is commented out in line 41. It can be deleted. But I haven’t done that, yet. There are several bits of code which are remains of the design process since this is work in progress. Code cleaning will be done when the main functionality is in place.

Back to addmpl where I embedded gui elements into the canvas. Taking out the matplotlib taskbar doesn’t change a thing as I wrote earlier, but to make sure it doesn’t bother the mainloop, it should be commented out. I may not put it back in, because I don’t see the point in needing it. It was just to see if it’s possible.

But option 2 relinquishes that control to the developer’s GUI app. You cannot use pyplot for option 2, which is what you are doing.

Is that so? In line 116 I create the canvas, which is derived from matplotlib’s backend’s FigureCanvasQTAgg and given to the QWidget at line 119. That’s the only part where both interact with each other. the rest is handle by matplotlib.

The error message says that Axes3D.figure.canvas is ‘None’ and that’s why mouse rotation is disabled.

It’s None because there is no content at that point, when it’s passed to the QWidget. It’s filled with content in line 38. So if matplotlib disables the mouse rotation by default, when the canvas is empty how do I prevent this disabling by default?

If I can’t, at what point do I have to pass the filled canvas to the QWidget? How does that impact the GUI itself?

If I can’t enable the mouse rotation by hand and I just can pass filled canvas around, do I have to build a work around with initialize it with an empty 2D canvas and replace it later with the filled 3D canvas? How’s the mouse rotation activated then?

In general, I wouldn’t have to enable the rotation if it wouldn’t be switch off for an empty canvas.

I’m going to consult your book, now, for different ways of coping with such things…

cheers,

Christian


“A little learning never caused anyone’s head to explode!”

“Ein wenig Lernen hat noch niemandens Kopf zum Explodieren gebracht!”

On Tuesday, April 28, 2015 8:28 PM, Benjamin Root <ben.root@…1304…> wrote:

One thing I see off the bat is your addmpl() method:

    def addmpl(self, fig):
        #FigureCanvas.__init__(self, fig)
        self.canvas = FigureCanvas(fig)
       
        Axes3D.mouse_init(self, rotate_btn=1, zoom_btn=2)
        self.mplvl.addWidget(self.canvas)
        self.canvas.draw()
        self.toolbar = NavigationToolbar(self.canvas, self.mplwindow, coordinates=True)
        self.mplvl.addWidget(self.toolbar)

You are calling Axes3D.mouse_init() on the Main object (that is self). That is completely wrong. It can only be called for the 3d axes objects.

Also, what I see happening here is some mixing up of how to do embedding. There are two approaches to embedding. 1) you can embedded GUI elements into your canvas widget, or 2) you can embed your canvas widget into your GUI app. The important distinction between the two is who controls the mainloop. In option 1 (and in matplotlib in general), pyplot will create the GUI app for you automatically (it is completely transparent to you) and kicks it off upon call to show(). But option 2 relinquishes that control to the developer’s GUI app. You cannot use pyplot for option 2, which is what you are doing. Rip out all of the pyplot stuff, and instantiate the Qt5 Figure object directly, and then obtain the axes objects from the figure object via calls to add_subplot(). You shouldn’t even need to do the whole mouse_init() stuff.

I now think this has nothing to do with Qt Designer. While I don’t specifically cover qt5 in my book, I do make all of these distinctions very clear in chapter 5 of my book “Interactive Applications using Matplotlib”.

Cheers!

Ben Root

On Tue, Apr 28, 2015 at 4:03 PM, Christian Ambros <ambrosc@…2693…> wrote:

Hi Benjamin,

I would do that if my task were my private stuff, but in this case it’s work-related and my boss wants me to use the designer and he already set a deadline, which, I already knew, is set to tight. I told him before, that it would be just a try but he sold it to his boss after some pressure. You know how the bosses’ bosses are, they don’t get the idea that innovation can’t be dictated. They don’t understand the concept that software is written and doesn’t come into existence out of nothing.

Without PyQt5 it’s working fine. I got the plots and they are gorgeous, but that doesn’t help when presenting to the bosses. If I just would know how to activate the 3d-draw’s mouse action again, by hand, than it has to last just some moments for the presentation, afterwards I have the time to examine and find a more robust solution.

Thanks for the effort.

cheers,

Christian


“A little learning never caused anyone’s head to explode!”

“Ein wenig Lernen hat noch niemandens Kopf zum Explodieren gebracht!”

On Tuesday, April 28, 2015 7:30 PM, Benjamin Root <ben.root@…1304…> wrote:

I think there is something wrong with the embedding code rather than there being an actual bug. I have embedded mplot3d stuff before (admittedly, not in qt5) with no problems. I haven’t had the time yet to examine your code to see what the potential issue is, though. I have also never used Qt designer, so I have no clue if there is something that it is doing that might be making things difficult.

I already know that the code you originally posted has errors in it. I would suggest first making a prototype without Qt Designer as a proof-of-concept, perhaps starting with one of our examples in the gallery?

Ben Root

On Tue, Apr 28, 2015 at 2:12 PM, Christian Ambros <ambrosc@…2693…> wrote:

Since there seems to be no progress with this issue, may I assume there isn’t any interest in it?

I took a further look around in the internet but couldn’t any solution.

It leads to an other question: How many users of matplotlib are using 3d-plots anyway? It we are just a few and there won’t be anyone who wants to embed it in PyQt5, than I can understand that this issue doesn’t concern no-one and I have to look somewhere else to find a 3d-plotting lib which is embedable.

cheers,

Christain


“A little learning never caused anyone’s head to explode!”

“Ein wenig Lernen hat noch niemandens Kopf zum Explodieren gebracht!”

On Tuesday, April 21, 2015 1:44 PM, Benjamin Root <ben.root@…1304…> wrote:

The addmpl() method isn’t right. You created a canvas object, assigned it to self.canvas, but then tried to call FigureCanvas.init(), passing it whatever object “self” is. What class is addmpl() a part of? What does it subclass?

On Tue, Apr 21, 2015 at 7:24 AM, Christian Ambros <ambrosc@…2693…> wrote:

Hi,

I embedded Ryan’s examble for PyQt5-matplotlib use into my App but I get the following error:

/usr/local/lib/python3.4/dist-packages/mpl_toolkits/mplot3d/axes3d.py:1009: UserWarning: Axes3D.figure.canvas is ‘None’, mouse rotation disabled. Set canvas then call Axes3D.mouse_init().
warnings.warn(‘Axes3D.figure.canvas is ‘None’, mouse rotation disabled. Set canvas then call Axes3D.mouse_init().’)

From Stackoverflow, which host to question about this, I know that mouse actions are disabled when the canvas is re-initialized by whatever.

The only position I do such an operation is in here:

def addmpl(self, fig):
self.canvas = FigureCanvas(fig)
#FigureCanvas.init(self, fig)
#Axes3D.mouse_init(self)
self.mplvl.addWidget(self.canvas)
self.canvas.draw()
self.toolbar = NavigationToolbar(self.canvas, self.mplwindow, coordinates=True)
self.mplvl.addWidget(self.toolbar)

On of the Stackoverflow suggestion says, that re initializing FigureCanvas should do the trick but I’ll get:

Traceback (most recent call last):
File “./ex_0.1.py”, line 145, in
main(sys.argv)
File “./ex_0.1.py”, line 53, in main
mainwindow.addmpl(fig1)
File “./ex_0.1.py”, line 116, in addmpl
FigureCanvas.init(self, fig)
File “/usr/local/lib/python3.4/dist-packages/matplotlib/backends/backend_qt5agg.py”, line 181, in init
FigureCanvasQT.init(self, figure)
File “/usr/local/lib/python3.4/dist-packages/matplotlib/backends/backend_qt5.py”, line 237, in init
super(FigureCanvasQT, self).init(figure=figure)
TypeError: super(type, obj): obj must be an instance or subtype of type

as follow-up error message.

just using Axes3D.mouse_init() , as suggested by matplotlib itself, leads to:

Traceback (most recent call last):
File “./ex_0.1.py”, line 146, in
main(sys.argv)
File “./ex_0.1.py”, line 53, in main
mainwindow.addmpl(fig1)
File “./ex_0.1.py”, line 118, in addmpl
Axes3D.mouse_init()
TypeError: mouse_init() missing 1 required positional argument: ‘self’

adding self leads to:

Traceback (most recent call last):
File “./ex_0.1.py”, line 146, in
main(sys.argv)
File “./ex_0.1.py”, line 53, in main
mainwindow.addmpl(fig1)
File “./ex_0.1.py”, line 118, in addmpl
Axes3D.mouse_init(self)
File “/usr/local/lib/python3.4/dist-packages/mpl_toolkits/mplot3d/axes3d.py”, line 1002, in mouse_init
canv = self.figure.canvas
AttributeError: ‘Main’ object has no attribute ‘figure’
./ex_0.1.py &

Maybe I’m adding those lines at the wrong place, but I could fined anything useful in the matplotlib documantation, that would help me out, either.

Any thougts that might help?

Cheers,

Christian


“A little learning never caused anyone’s head to explode!”

“Ein wenig Lernen hat noch niemandens Kopf zum Explodieren gebracht!”


BPM Camp - Free Virtual Workshop May 6th at 10am PDT/1PM EDT

Develop your own process in accordance with the BPMN 2 standard

Learn Process modeling best practices with Bonita BPM through live exercises

http://www.bonitasoft.com/be-part-of-it/events/bpm-camp-virtual- event?utm_

source=Sourceforge_BPM_Camp_5_6_15&utm_medium=email&utm_campaign=VA_SF


Matplotlib-users mailing list

Matplotlib-users@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/matplotlib-users


One dashboard for servers and applications across Physical-Virtual-Cloud

Widest out-of-the-box monitoring support with 50+ applications

Performance metrics, stats and reports that give you Actionable Insights

Deep dive visibility with transaction tracing using APM Insight.

http://ad.doubleclick.net/ddm/clk/290420510;117567292;y


Matplotlib-users mailing list

Matplotlib-users@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/matplotlib-users

I just managed to read Thomas post on stackoverflow citation. Yes, it covers, exactly what I found. This would have help if I would have found earlier.

@Ben:

As I said, it’s nice to see such implementation but it’s offside the task and offside the suggested components to use. It doesn’t cover the issues I had to deal with. I am not disregarding it! But if you have to use a gui built with Qt Designer to decouple gui design which might be given as task to someone else so you can focus on implementing the functional parts, and somewhat later, someone shall improve the design, this way of creating things has the advantage that proceeding the work, if one stops working for the project, is as easy as it should be.

You can split the work to gain more development speed, which is an advantage, too. Keeping the functional part away from the gui itself, enables one to improve the design without touching the functional code and without changing it’s revision number if version control systems are used. Code recycling is a third one. I don’t see any drawback, yet, if you don’t take into account that they may be something missing in the additional libraries and the gab might cause problems not solvable.

But that’s not the topic of this thread.

Yes, Qt Designer just provides only the framework, which than can be filled with content, for example, coming from matplotlib. Having just a dummy widget to fill as interface pays respect to being most flexible.

No, having to re-initiate the mouse because it is disabled from matplotlib due to an empty canvas is not wrong but a design decision. One could have easily decided to not disable it, but the ones who wrote that part might have thought, that if the canvas is empty,you don’t need axes to rotate. They discovered that this thoughts lead to problems and build a workaround enabling to switch it on again, rather than change the default. That’s ok for me, because it was there decision. I would have like to find any hint about that in books, tutorials or stuff like that, but that seems to be a not that often regarded problem.

I took your objection about adding the axis before the canvas is build and changed the code so after creating the figure I’m adding it to the canvas so it won’t be empty anymore. This is a better way and is avoiding the mouse issue.

Yes, it’s clear now, but that is what I asked for in the first place. I mentioned that there may be something out of correct order. Now we know what it was and I fixed it.

I like this kind of discussion which clarify the understanding, because you can get them from books. So thank you very much for the talk.

I think I’m going to review the part where all the figures are created and will put them into a method which builds them if needed. That would clean up the design and the code, too.

But that’s something for after the deadline, for which the code can be as ugly as it is. It just has to work to get my job contract renewed.

cheers,

Christian

···


“A little learning never caused anyone’s head to explode!”

“Ein wenig Lernen hat noch niemandens Kopf zum Explodieren gebracht!”