[Qgis-user] problem with matplotlib in context with QGIS processing plugins

Manfred Strahlhofer manfred.strahlhofer at gmail.com
Wed Feb 15 17:11:30 PST 2023


Hi Johannes!

Thanks for your reply!
Unfortunately the plt.show() does not show anything when called from the
processing plugin's "processAlgorithm()" function.
When called from the python console, it works fine instead.
I also detected that "fig.tight_layout()" stucks QGIS, when used in a
processing plugin.
But fortunately, in the meantime I've also detected, that  I can save the
whole plot to a PDF file by using "plt.savefig(pdf_name)".
And that's already a great benefit for me.

Best regards


Am Mi., 15. Feb. 2023 um 09:39 Uhr schrieb Johannes Kröger (WhereGroup) <
johannes.kroeger at wheregroup.com>:

> Hi Manfred,
>
> not what you asked but: Are you sure you need a second QMainWindow or
> maybe a non-modal QDialog or a QDockWidget would be sufficient? Those might
> be easier to use. Or maybe even just using the window that plt.show()
> creates?
>
> Cheers, Hannes
> Am 14.02.23 um 22:11 schrieb Manfred Strahlhofer via QGIS-User:
>
> Hello!
>
> There is a problem when using "matplotlib.backends" and drawing plots when
> called from a QGIS processing plugin. I am using the following test code:
> # sample code below draws a canvas with a plot
> # however closing this plot canvas crashes the plugin (cannot be restarted)
> # and further qgis can't be closed with the X button
> # originates from:
> https://matplotlib.org/stable/gallery/user_interfaces/embedding_in_qt_sgskip.html#sphx-glr-gallery-user-interfaces-embedding-in-qt-sgskip-py
> import sys
> import time
> import numpy as np
> from matplotlib.backends.qt_compat import QtWidgets
> from matplotlib.backends.backend_qtagg import (
>     FigureCanvas, NavigationToolbar2QT as NavigationToolbar)
> from matplotlib.figure import Figure
> class ApplicationWindow(QtWidgets.QMainWindow):
>     def __init__(self):
>         super().__init__()
>         self._main = QtWidgets.QWidget()
>         self.setCentralWidget(self._main)
>         layout = QtWidgets.QVBoxLayout(self._main)
>         static_canvas = FigureCanvas(Figure(figsize=(5, 3)))
>         # Ideally one would use self.addToolBar here, but it is slightly
>         # incompatible between PyQt6 and other bindings, so we just add
> the
>         # toolbar as a plain widget instead.
>         layout.addWidget(NavigationToolbar(static_canvas, self))
>         layout.addWidget(static_canvas)
>         dynamic_canvas = FigureCanvas(Figure(figsize=(5, 3)))
>         layout.addWidget(dynamic_canvas)
>         layout.addWidget(NavigationToolbar(dynamic_canvas, self))
>         self._static_ax = static_canvas.figure.subplots()
>         t = np.linspace(0, 10, 501)
>         self._static_ax.plot(t, np.tan(t), ".")
>         self._dynamic_ax = dynamic_canvas.figure.subplots()
>         t = np.linspace(0, 10, 101)
>         # Set up a Line2D.
>         self._line, = self._dynamic_ax.plot(t, np.sin(t + time.time()))
>         self._timer = dynamic_canvas.new_timer(50)
>         self._timer.add_callback(self._update_canvas)
>         self._timer.start()
>     def _update_canvas(self):
>         t = np.linspace(0, 10, 101)
>         # Shift the sinusoid as a function of time.
>         self._line.set_data(t, np.sin(t + time.time()))
>         self._line.figure.canvas.draw()
> def test_plot():
>     # Check whether there is already a running QApplication (e.g., if
> running
>     # from an IDE).
>     #qapp = QtWidgets.QApplication.instance()   this stucks QGIS!
>     #if not qapp:
>     qapp = QtWidgets.QApplication(sys.argv) # this stucks QGIS main
> window when the dialog's close button is pressed
>     app = ApplicationWindow()
>     app.show()
>     app.activateWindow()
>     app.raise_()
>     qapp.exec()
>     #app.exit()
>
> When using "qapp = QtWidgets.QApplication.instance()", QGIS stucks
> completely and no plot is shown ever.
> When using "qapp = QtWidgets.QApplication(sys.argv)", the plot is shown
> and updated correctly. But when pressing the "close button" of the plot
> window, there are some strange effects (cannot start a plugin again) and
> the "close button" of the QGIS main window is disabled. Have to shut down
> qgis-bin.exe from the windows task-manager.
> I am using:
> QGIS Version 3.24.1 Tisler
> matplotlib 3.5.1
> Windows 10
>
> Anybody have a solution for this problem?
> Thanks a lot.
>
>
>
>
> _______________________________________________
> QGIS-User mailing listQGIS-User at lists.osgeo.org
> List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user
>
> --
> Johannes Kröger / GIS-Entwickler/-Berater
>
> **********************************************
> FOSSGIS Konferenz
> 15.-18. März 2023 in Berlinhttps://fossgis-konferenz.de/2023/
>
> WhereGroup-Beiträge auf der FOSSGIShttps://wheregroup.com/unternehmen/aktuelles/
> **********************************************
>
> WhereGroup GmbH
> Grevenweg 89
> 20537 Hamburg
> Germany
>
> Tel: +49 (0)228 / 90 90 38 - 36
> Fax: +49 (0)228 / 90 90 38 - 11
> johannes.kroeger at wheregroup.comwww.wheregroup.com
> Geschäftsführer:
> Olaf Knopp, Peter Stamm
> Amtsgericht Bonn, HRB 9885
> -------------------------------
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20230216/a4a738dc/attachment.htm>


More information about the QGIS-User mailing list