<div dir="ltr"><div class="gmail_default" style="font-family:verdana,sans-serif;font-size:small">Hello!</div><div class="gmail_default" style="font-family:verdana,sans-serif;font-size:small"><br></div><div class="gmail_default" style="font-family:verdana,sans-serif;font-size:small">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:</div><div class="gmail_default" style="font-family:verdana,sans-serif;font-size:small">


<div style="color:rgb(212,212,212);background-color:rgb(30,30,30);font-family:Consolas,"Courier New",monospace;font-weight:normal;font-size:14px;line-height:19px;white-space:pre"><div><span style="color:rgb(106,153,85)"># sample code below draws a canvas with a plot</span></div><div><span style="color:rgb(106,153,85)"># however closing this plot canvas crashes the plugin (cannot be restarted)</span></div><div><span style="color:rgb(106,153,85)"># and further qgis can't be closed with the X button</span></div><div><span style="color:rgb(106,153,85)"># originates from: <a href="https://matplotlib.org/stable/gallery/user_interfaces/embedding_in_qt_sgskip.html#sphx-glr-gallery-user-interfaces-embedding-in-qt-sgskip-py">https://matplotlib.org/stable/gallery/user_interfaces/embedding_in_qt_sgskip.html#sphx-glr-gallery-user-interfaces-embedding-in-qt-sgskip-py</a></span></div><div><span style="color:rgb(197,134,192)">import</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(78,201,176)">sys</span></div><div><span style="color:rgb(197,134,192)">import</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(78,201,176)">time</span></div><br><div><span style="color:rgb(197,134,192)">import</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(78,201,176)">numpy</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(197,134,192)">as</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(78,201,176)">np</span></div><br><div><span style="color:rgb(197,134,192)">from</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(78,201,176)">matplotlib</span><span style="color:rgb(212,212,212)">.</span><span style="color:rgb(78,201,176)">backends</span><span style="color:rgb(212,212,212)">.</span><span style="color:rgb(78,201,176)">qt_compat</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(197,134,192)">import</span><span style="color:rgb(212,212,212)"> QtWidgets</span></div><div><span style="color:rgb(197,134,192)">from</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(78,201,176)">matplotlib</span><span style="color:rgb(212,212,212)">.</span><span style="color:rgb(78,201,176)">backends</span><span style="color:rgb(212,212,212)">.</span><span style="color:rgb(78,201,176)">backend_qtagg</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(197,134,192)">import</span><span style="color:rgb(212,212,212)"> (</span></div><div><span style="color:rgb(212,212,212)">    FigureCanvas, NavigationToolbar2QT </span><span style="color:rgb(197,134,192)">as</span><span style="color:rgb(212,212,212)"> NavigationToolbar)</span></div><div><span style="color:rgb(197,134,192)">from</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(78,201,176)">matplotlib</span><span style="color:rgb(212,212,212)">.</span><span style="color:rgb(78,201,176)">figure</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(197,134,192)">import</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(78,201,176)">Figure</span></div><br><br><div><span style="color:rgb(86,156,214)">class</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(78,201,176)">ApplicationWindow</span><span style="color:rgb(212,212,212)">(</span><span style="color:rgb(78,201,176)">QtWidgets</span><span style="color:rgb(212,212,212)">.</span><span style="color:rgb(78,201,176)">QMainWindow</span><span style="color:rgb(212,212,212)">):</span></div><div><span style="color:rgb(212,212,212)">    </span><span style="color:rgb(86,156,214)">def</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(220,220,170)">__init__</span><span style="color:rgb(212,212,212)">(</span><span style="color:rgb(156,220,254)">self</span><span style="color:rgb(212,212,212)">):</span></div><div><span style="color:rgb(212,212,212)">        </span><span style="color:rgb(78,201,176)">super</span><span style="color:rgb(212,212,212)">().</span><span style="color:rgb(220,220,170)">__init__</span><span style="color:rgb(212,212,212)">()</span></div><div><span style="color:rgb(212,212,212)">        </span><span style="color:rgb(156,220,254)">self</span><span style="color:rgb(212,212,212)">.</span><span style="color:rgb(156,220,254)">_main</span><span style="color:rgb(212,212,212)"> = QtWidgets.QWidget()</span></div><div><span style="color:rgb(212,212,212)">        </span><span style="color:rgb(156,220,254)">self</span><span style="color:rgb(212,212,212)">.setCentralWidget(</span><span style="color:rgb(156,220,254)">self</span><span style="color:rgb(212,212,212)">.</span><span style="color:rgb(156,220,254)">_main</span><span style="color:rgb(212,212,212)">)</span></div><div><span style="color:rgb(212,212,212)">        </span><span style="color:rgb(156,220,254)">layout</span><span style="color:rgb(212,212,212)"> = QtWidgets.QVBoxLayout(</span><span style="color:rgb(156,220,254)">self</span><span style="color:rgb(212,212,212)">.</span><span style="color:rgb(156,220,254)">_main</span><span style="color:rgb(212,212,212)">)</span></div><br><div><span style="color:rgb(212,212,212)">        </span><span style="color:rgb(156,220,254)">static_canvas</span><span style="color:rgb(212,212,212)"> = FigureCanvas(</span><span style="color:rgb(78,201,176)">Figure</span><span style="color:rgb(212,212,212)">(</span><span style="color:rgb(156,220,254)">figsize</span><span style="color:rgb(212,212,212)">=(</span><span style="color:rgb(181,206,168)">5</span><span style="color:rgb(212,212,212)">, </span><span style="color:rgb(181,206,168)">3</span><span style="color:rgb(212,212,212)">)))</span></div><div><span style="color:rgb(212,212,212)">        </span><span style="color:rgb(106,153,85)"># Ideally one would use self.addToolBar here, but it is slightly</span></div><div><span style="color:rgb(212,212,212)">        </span><span style="color:rgb(106,153,85)"># incompatible between PyQt6 and other bindings, so we just add the</span></div><div><span style="color:rgb(212,212,212)">        </span><span style="color:rgb(106,153,85)"># toolbar as a plain widget instead.</span></div><div><span style="color:rgb(212,212,212)">        </span><span style="color:rgb(156,220,254)">layout</span><span style="color:rgb(212,212,212)">.addWidget(NavigationToolbar(</span><span style="color:rgb(156,220,254)">static_canvas</span><span style="color:rgb(212,212,212)">, </span><span style="color:rgb(156,220,254)">self</span><span style="color:rgb(212,212,212)">))</span></div><div><span style="color:rgb(212,212,212)">        </span><span style="color:rgb(156,220,254)">layout</span><span style="color:rgb(212,212,212)">.addWidget(</span><span style="color:rgb(156,220,254)">static_canvas</span><span style="color:rgb(212,212,212)">)</span></div><br><div><span style="color:rgb(212,212,212)">        </span><span style="color:rgb(156,220,254)">dynamic_canvas</span><span style="color:rgb(212,212,212)"> = FigureCanvas(</span><span style="color:rgb(78,201,176)">Figure</span><span style="color:rgb(212,212,212)">(</span><span style="color:rgb(156,220,254)">figsize</span><span style="color:rgb(212,212,212)">=(</span><span style="color:rgb(181,206,168)">5</span><span style="color:rgb(212,212,212)">, </span><span style="color:rgb(181,206,168)">3</span><span style="color:rgb(212,212,212)">)))</span></div><div><span style="color:rgb(212,212,212)">        </span><span style="color:rgb(156,220,254)">layout</span><span style="color:rgb(212,212,212)">.addWidget(</span><span style="color:rgb(156,220,254)">dynamic_canvas</span><span style="color:rgb(212,212,212)">)</span></div><div><span style="color:rgb(212,212,212)">        </span><span style="color:rgb(156,220,254)">layout</span><span style="color:rgb(212,212,212)">.addWidget(NavigationToolbar(</span><span style="color:rgb(156,220,254)">dynamic_canvas</span><span style="color:rgb(212,212,212)">, </span><span style="color:rgb(156,220,254)">self</span><span style="color:rgb(212,212,212)">))</span></div><br><div><span style="color:rgb(212,212,212)">        </span><span style="color:rgb(156,220,254)">self</span><span style="color:rgb(212,212,212)">.</span><span style="color:rgb(156,220,254)">_static_ax</span><span style="color:rgb(212,212,212)"> = </span><span style="color:rgb(156,220,254)">static_canvas</span><span style="color:rgb(212,212,212)">.figure.subplots()</span></div><div><span style="color:rgb(212,212,212)">        </span><span style="color:rgb(156,220,254)">t</span><span style="color:rgb(212,212,212)"> = </span><span style="color:rgb(78,201,176)">np</span><span style="color:rgb(212,212,212)">.</span><span style="color:rgb(220,220,170)">linspace</span><span style="color:rgb(212,212,212)">(</span><span style="color:rgb(181,206,168)">0</span><span style="color:rgb(212,212,212)">, </span><span style="color:rgb(181,206,168)">10</span><span style="color:rgb(212,212,212)">, </span><span style="color:rgb(181,206,168)">501</span><span style="color:rgb(212,212,212)">)</span></div><div><span style="color:rgb(212,212,212)">        </span><span style="color:rgb(156,220,254)">self</span><span style="color:rgb(212,212,212)">.</span><span style="color:rgb(156,220,254)">_static_ax</span><span style="color:rgb(212,212,212)">.plot(</span><span style="color:rgb(156,220,254)">t</span><span style="color:rgb(212,212,212)">, </span><span style="color:rgb(78,201,176)">np</span><span style="color:rgb(212,212,212)">.</span><span style="color:rgb(156,220,254)">tan</span><span style="color:rgb(212,212,212)">(</span><span style="color:rgb(156,220,254)">t</span><span style="color:rgb(212,212,212)">), </span><span style="color:rgb(206,145,120)">"."</span><span style="color:rgb(212,212,212)">)</span></div><br><div><span style="color:rgb(212,212,212)">        </span><span style="color:rgb(156,220,254)">self</span><span style="color:rgb(212,212,212)">.</span><span style="color:rgb(156,220,254)">_dynamic_ax</span><span style="color:rgb(212,212,212)"> = </span><span style="color:rgb(156,220,254)">dynamic_canvas</span><span style="color:rgb(212,212,212)">.figure.subplots()</span></div><div><span style="color:rgb(212,212,212)">        </span><span style="color:rgb(156,220,254)">t</span><span style="color:rgb(212,212,212)"> = </span><span style="color:rgb(78,201,176)">np</span><span style="color:rgb(212,212,212)">.</span><span style="color:rgb(220,220,170)">linspace</span><span style="color:rgb(212,212,212)">(</span><span style="color:rgb(181,206,168)">0</span><span style="color:rgb(212,212,212)">, </span><span style="color:rgb(181,206,168)">10</span><span style="color:rgb(212,212,212)">, </span><span style="color:rgb(181,206,168)">101</span><span style="color:rgb(212,212,212)">)</span></div><div><span style="color:rgb(212,212,212)">        </span><span style="color:rgb(106,153,85)"># Set up a Line2D.</span></div><div><span style="color:rgb(212,212,212)">        </span><span style="color:rgb(156,220,254)">self</span><span style="color:rgb(212,212,212)">.</span><span style="color:rgb(156,220,254)">_line</span><span style="color:rgb(212,212,212)">, = </span><span style="color:rgb(156,220,254)">self</span><span style="color:rgb(212,212,212)">.</span><span style="color:rgb(156,220,254)">_dynamic_ax</span><span style="color:rgb(212,212,212)">.plot(</span><span style="color:rgb(156,220,254)">t</span><span style="color:rgb(212,212,212)">, </span><span style="color:rgb(78,201,176)">np</span><span style="color:rgb(212,212,212)">.</span><span style="color:rgb(156,220,254)">sin</span><span style="color:rgb(212,212,212)">(</span><span style="color:rgb(156,220,254)">t</span><span style="color:rgb(212,212,212)"> + </span><span style="color:rgb(78,201,176)">time</span><span style="color:rgb(212,212,212)">.</span><span style="color:rgb(220,220,170)">time</span><span style="color:rgb(212,212,212)">()))</span></div><div><span style="color:rgb(212,212,212)">        </span><span style="color:rgb(156,220,254)">self</span><span style="color:rgb(212,212,212)">.</span><span style="color:rgb(156,220,254)">_timer</span><span style="color:rgb(212,212,212)"> = </span><span style="color:rgb(156,220,254)">dynamic_canvas</span><span style="color:rgb(212,212,212)">.new_timer(</span><span style="color:rgb(181,206,168)">50</span><span style="color:rgb(212,212,212)">)</span></div><div><span style="color:rgb(212,212,212)">        </span><span style="color:rgb(156,220,254)">self</span><span style="color:rgb(212,212,212)">.</span><span style="color:rgb(156,220,254)">_timer</span><span style="color:rgb(212,212,212)">.add_callback(</span><span style="color:rgb(156,220,254)">self</span><span style="color:rgb(212,212,212)">.</span><span style="color:rgb(220,220,170)">_update_canvas</span><span style="color:rgb(212,212,212)">)</span></div><div><span style="color:rgb(212,212,212)">        </span><span style="color:rgb(156,220,254)">self</span><span style="color:rgb(212,212,212)">.</span><span style="color:rgb(156,220,254)">_timer</span><span style="color:rgb(212,212,212)">.start()</span></div><br><div><span style="color:rgb(212,212,212)">    </span><span style="color:rgb(86,156,214)">def</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(220,220,170)">_update_canvas</span><span style="color:rgb(212,212,212)">(</span><span style="color:rgb(156,220,254)">self</span><span style="color:rgb(212,212,212)">):</span></div><div><span style="color:rgb(212,212,212)">        </span><span style="color:rgb(156,220,254)">t</span><span style="color:rgb(212,212,212)"> = </span><span style="color:rgb(78,201,176)">np</span><span style="color:rgb(212,212,212)">.</span><span style="color:rgb(220,220,170)">linspace</span><span style="color:rgb(212,212,212)">(</span><span style="color:rgb(181,206,168)">0</span><span style="color:rgb(212,212,212)">, </span><span style="color:rgb(181,206,168)">10</span><span style="color:rgb(212,212,212)">, </span><span style="color:rgb(181,206,168)">101</span><span style="color:rgb(212,212,212)">)</span></div><div><span style="color:rgb(212,212,212)">        </span><span style="color:rgb(106,153,85)"># Shift the sinusoid as a function of time.</span></div><div><span style="color:rgb(212,212,212)">        </span><span style="color:rgb(156,220,254)">self</span><span style="color:rgb(212,212,212)">.</span><span style="color:rgb(156,220,254)">_line</span><span style="color:rgb(212,212,212)">.set_data(</span><span style="color:rgb(156,220,254)">t</span><span style="color:rgb(212,212,212)">, </span><span style="color:rgb(78,201,176)">np</span><span style="color:rgb(212,212,212)">.</span><span style="color:rgb(156,220,254)">sin</span><span style="color:rgb(212,212,212)">(</span><span style="color:rgb(156,220,254)">t</span><span style="color:rgb(212,212,212)"> + </span><span style="color:rgb(78,201,176)">time</span><span style="color:rgb(212,212,212)">.</span><span style="color:rgb(220,220,170)">time</span><span style="color:rgb(212,212,212)">()))</span></div><div><span style="color:rgb(212,212,212)">        </span><span style="color:rgb(156,220,254)">self</span><span style="color:rgb(212,212,212)">.</span><span style="color:rgb(156,220,254)">_line</span><span style="color:rgb(212,212,212)">.figure.canvas.draw()</span></div><br><br><div><span style="color:rgb(86,156,214)">def</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(220,220,170)">test_plot</span><span style="color:rgb(212,212,212)">():</span></div><br><div><span style="color:rgb(212,212,212)">    </span><span style="color:rgb(106,153,85)"># Check whether there is already a running QApplication (e.g., if running</span></div><div><span style="color:rgb(212,212,212)">    </span><span style="color:rgb(106,153,85)"># from an IDE).</span></div><div><span style="color:rgb(212,212,212)">    </span><span style="color:rgb(106,153,85)">#qapp = QtWidgets.QApplication.instance()   this stucks QGIS!</span></div><div><span style="color:rgb(212,212,212)">    </span><span style="color:rgb(106,153,85)">#if not qapp:</span></div><div><span style="color:rgb(212,212,212)">    </span><span style="color:rgb(156,220,254)">qapp</span><span style="color:rgb(212,212,212)"> = QtWidgets.QApplication(</span><span style="color:rgb(78,201,176)">sys</span><span style="color:rgb(212,212,212)">.</span><span style="color:rgb(156,220,254)">argv</span><span style="color:rgb(212,212,212)">) </span><span style="color:rgb(106,153,85)"># this stucks QGIS main window when the dialog's close button is pressed</span></div><br><div><span style="color:rgb(212,212,212)">    </span><span style="color:rgb(156,220,254)">app</span><span style="color:rgb(212,212,212)"> = </span><span style="color:rgb(78,201,176)">ApplicationWindow</span><span style="color:rgb(212,212,212)">()</span></div><div><span style="color:rgb(212,212,212)">    </span><span style="color:rgb(156,220,254)">app</span><span style="color:rgb(212,212,212)">.show()</span></div><div><span style="color:rgb(212,212,212)">    </span><span style="color:rgb(156,220,254)">app</span><span style="color:rgb(212,212,212)">.activateWindow()</span></div><div><span style="color:rgb(212,212,212)">    </span><span style="color:rgb(156,220,254)">app</span><span style="color:rgb(212,212,212)">.raise_()</span></div><div><span style="color:rgb(212,212,212)">    </span><span style="color:rgb(156,220,254)">qapp</span><span style="color:rgb(212,212,212)">.exec()</span></div><div><span style="color:rgb(212,212,212)">    </span><span style="color:rgb(106,153,85)">#app.exit()</span></div><br></div>



</div><div class="gmail_default" style="font-family:verdana,sans-serif;font-size:small"><br></div><div class="gmail_default" style="font-family:verdana,sans-serif;font-size:small">When using "<span style="color:rgb(0,0,0)">qapp = QtWidgets.QApplication.instance()</span>", QGIS stucks completely and no plot is shown ever.</div><div class="gmail_default" style="font-family:verdana,sans-serif;font-size:small">When using 
<span style="color:rgb(0,0,0)"><span style="background-color:rgb(255,255,255)">"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.</span></span></div><div class="gmail_default" style="font-family:verdana,sans-serif;font-size:small"><span style="color:rgb(0,0,0)"><span style="background-color:rgb(255,255,255)">I am using:</span></span></div><div class="gmail_default" style="font-family:verdana,sans-serif;font-size:small"><span style="color:rgb(0,0,0)"><span style="background-color:rgb(255,255,255)">QGIS Version 3.24.1 Tisler</span></span></div><div class="gmail_default" style="font-family:verdana,sans-serif;font-size:small"><span style="color:rgb(0,0,0)"><span style="background-color:rgb(255,255,255)">matplotlib 3.5.1<br></span></span></div><div class="gmail_default" style="font-family:verdana,sans-serif;font-size:small"><span style="color:rgb(0,0,0)"><span style="background-color:rgb(255,255,255)">Windows 10<br></span></span></div><div class="gmail_default" style="font-family:verdana,sans-serif;font-size:small"><span style="color:rgb(0,0,0)"><span style="background-color:rgb(255,255,255)"><br></span></span></div><div class="gmail_default" style="font-family:verdana,sans-serif;font-size:small"><span style="color:rgb(0,0,0)"><span style="background-color:rgb(255,255,255)">Anybody have a solution for this problem?</span></span></div><div class="gmail_default" style="font-family:verdana,sans-serif;font-size:small"><span style="color:rgb(0,0,0)"><span style="background-color:rgb(255,255,255)">Thanks a lot.<br></span></span>

</div><div class="gmail_default" style="font-family:verdana,sans-serif;font-size:small"><br></div><div class="gmail_default" style="font-family:verdana,sans-serif;font-size:small"><span style="background-color:rgb(255,255,0)"><span style=""></span></span><br></div><div class="gmail_default" style="font-family:verdana,sans-serif;font-size:small"><br></div></div>