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 <ambr...@ymail.com> 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.r...@ou.edu>
> 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 <ambr...@ymail.com>
> 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.r...@ou.edu>
> 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 <ambr...@ymail.com>
> 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.r...@ou.edu>
> 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 <ambr...@ymail.com>
> 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.r...@ou.edu>
> 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 <ambr...@ymail.com>
> 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 <module>
>     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 <module>
>     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 <module>
>     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
>
>
>
>
>
>
>
>
>
>
>
------------------------------------------------------------------------------
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

Reply via email to