.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/user_interfaces/pylab_with_gtk4_sgskip.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. meta:: :keywords: codex .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_gallery_user_interfaces_pylab_with_gtk4_sgskip.py: ================ pyplot with GTK4 ================ An example of how to use pyplot to manage your figure windows, but modify the GUI by accessing the underlying GTK widgets. .. GENERATED FROM PYTHON SOURCE LINES 9-53 .. code-block:: Python import matplotlib matplotlib.use('GTK4Agg') # or 'GTK4Cairo' import gi import matplotlib.pyplot as plt gi.require_version('Gtk', '4.0') from gi.repository import Gtk fig, ax = plt.subplots() ax.plot([1, 2, 3], 'ro-', label='easy as 1 2 3') ax.plot([1, 4, 9], 'gs--', label='easy as 1 2 3 squared') ax.legend() manager = fig.canvas.manager # you can access the window or vbox attributes this way toolbar = manager.toolbar vbox = manager.vbox # now let's add a button to the toolbar button = Gtk.Button(label='Click me') button.connect('clicked', lambda button: print('hi mom')) button.set_tooltip_text('Click me for fun and profit') toolbar.append(button) # now let's add a widget to the vbox label = Gtk.Label() label.set_markup('Drag mouse over axes for position') vbox.insert_child_after(label, fig.canvas) def update(event): if event.xdata is None: label.set_markup('Drag mouse over axes for position') else: label.set_markup( f'x,y=({event.xdata}, {event.ydata})') fig.canvas.mpl_connect('motion_notify_event', update) plt.show() .. _sphx_glr_download_gallery_user_interfaces_pylab_with_gtk4_sgskip.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: pylab_with_gtk4_sgskip.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: pylab_with_gtk4_sgskip.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_