0

Can anybody show a simple example with basic functionality of embedding matplotlib in PyQt5?

There is PyQt4 example available in official docs. PyQt4 will soon be outdated:

Digia have announced that support for Qt v4 will cease at the end of 2015. PyQt5 and Qt v5 are strongly recommended for all new development.

It could be any general example, say, Qt5 QMainWindow/QWidget + matplotlib line plot.

Bad
  • 4,967
  • 4
  • 34
  • 50
  • 1
    Stack Overflow is not a tutorial site. It is for specific questions about specific pieces of code, not "*somebody write some code for me, for free, to the following specifications.*" – MattDMo Mar 30 '15 at 22:33
  • This is mostly the same in Qt5: `curl -L http://matplotlib.org/examples/user_interfaces/embedding_in_qt4.py | sed -r 's@qt4_compat@qt_compat@g;s@([Qq]t)4@\15@g;s@QtGui@QtWidgets@g' | python` – Cilyan Mar 30 '15 at 22:55
  • 1
    @MattDMo Firstly, I do not want somebody to write a code for me, I write code only by myself. I asked for simple, short, general example which I can base my code upon. Secondly, say a person wants to make a matplotlib plot in PyQt5 and finds no available example in official docs. Don't you think it would be nice for this site to have such an example available for anybody interested? By the way, I read and use this site as a tutorial to a lot of programming topics as well. Do you think I should stop doing this? – Bad Mar 30 '15 at 23:16
  • mpl + Qt5 is only supported for python3. The qt4 backend is actually implemented as a subclass of the qt5 backend. – tacaswell Mar 30 '15 at 23:45
  • @MattDMo: `"Stack Overflow is not a tutorial site."` For example, it's explicitly stated here [`Stack Overflow F# Tutorial`](http://stackoverflow.com/tags/f%23/info). So it's a tutorial site also. :)) – Bad Apr 02 '16 at 15:03

1 Answers1

3

what you might be looking for, can be found here: embedded matplotlib in PyQt. It's a blog of one of matplotlib's developers. It is stil written for PyQt4 but it turned out that one just has to change all PyQt4's to 5 and replace QtGui with QtWidtgets. I talked to Ryan and send him the updated code. He was pleased to see that there are people who aknowledge his effort and will updated his tutorial.

Since I had the same problem and was looking around for some solutions to get a start, I found zedcode PyQt5 Introductory Tutorial which doesn't cover matplotlib but can be combined and helps to understand.

Finally, let me say something aobut what one has to do to get matplotlib plots into a Gui. Ryan's tut points out, that one has to build a normal QWidget which will hold the canvas. You can do that with Qt Creator. The part where gui and matplotlib join is the following:

def addmpl(self, fig):
    self.canvas = FigureCanvas(fig)
    self.mplvl.addWidget(self.canvas)
    self.canvas.draw()

mplvl is just the empty QWidget which gets the FigureCanvas and is shown afterwards. For my own app, I wanted to show a 3D-plot, but there are some issues with dropping the mouse support, so there could be some problems left. All 2D plots turned out to be fully functional.

Have a try.

Christian

strpeter
  • 2,562
  • 3
  • 27
  • 48
camaro
  • 118
  • 12