Matplotlib with QtQuick (QML)

Heads up! You've already completed this tutorial.

gpbak9611300 | 2021-05-23 13:17:37 UTC | #1

Hello! Is there any chance to find example (in the book, or in this site/forum) how to make the simplest plot using matplotlib but with QtQuick in QML? I'm really confused about the basics, I want to add really simple graph to new window.


martin | 2021-05-24 09:04:09 UTC | #2

Hi @gpbak9611300 welcome to the forum!

I unfortunately don't have an example for this yet, and am not 100% sure it is possible without quite a bit of work. The matplotlib Qt support is based around the Qt Widgets API, which is entirely separate from QML UIs, so you can't use the matplotlib Qt widgets in your QML application.

However, the matplotlib backend actually just generates an image and renders that onto a widget's pixmap. It might be possible to do this, applying the resulting image to a pixmap object in a QML view -- I think you'd effectively need to write a custom backend for matplotlib though.

Since it's a simple plot, would it be possible to use QtGraph to plot it? That's native and available in QML.


gpbak9611300 | 2021-05-24 15:43:56 UTC | #3

@martin Thanks for the info!

Actually I've found backend for QML (https://pypi.org/project/matplotlib-backend-qtquick/) but using it... its quite complicated for me (a newbie in QML). Anywany I'll try, and share the result ;)

But I'm wondering... Is there a way to open QtWidgets (or just other... window like in some IDE) in external window or externall app, and still have active QML app?


martin | 2021-05-25 19:44:14 UTC | #4

Hi @gpbak9611300, nice find!

Having looking into adding widgets to QML apps, apparently it's not possible -- there were ways to embed widgets in earlier versions of QML but they've been removed.


Over 10,000 developers have bought Create GUI Applications with Python & Qt!
Create GUI Applications with Python & Qt6
Take a look

Downloadable ebook (PDF, ePub) & Complete Source code

Also available from Payhip , Gumroad , Leanpub and Amazon Paperback

[[ discount.discount_pc ]]% OFF for the next [[ discount.duration ]] [[discount.description ]] with the code [[ discount.coupon_code ]]

Purchasing Power Parity

Developers in [[ country ]] get [[ discount.discount_pc ]]% OFF on all books & courses with code [[ discount.coupon_code ]]
Well done, you've finished this tutorial! Mark As Complete
[[ user.completed.length ]] completed [[ user.streak+1 ]] day streak

Matplotlib with QtQuick (QML) was written by Martin Fitzpatrick .

Martin Fitzpatrick has been developing Python/Qt apps for 8 years. Building desktop applications to make data-analysis tools more user-friendly, Python was the obvious choice. Starting with Tk, later moving to wxWidgets and finally adopting PyQt.