.. only:: html
.. note::
:class: sphx-glr-download-link-note
Click :ref:`here ` to download the full example code
.. rst-class:: sphx-glr-example-title
.. _sphx_glr_gallery_user_interfaces_pylab_with_gtk_sgskip.py:
===============
pyplot with GTK
===============
An example of how to use pyplot to manage your figure windows, but modify the
GUI by accessing the underlying GTK widgets.
.. code-block:: default
import matplotlib
matplotlib.use('GTK3Agg') # or 'GTK3Cairo'
import matplotlib.pyplot as plt
import gi
gi.require_version('Gtk', '3.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.show()
button.connect('clicked', lambda button: print('hi mom'))
toolitem = Gtk.ToolItem()
toolitem.show()
toolitem.set_tooltip_text('Click me for fun and profit')
toolitem.add(button)
pos = 8 # where to insert this in the toolbar
toolbar.insert(toolitem, pos)
# now let's add a widget to the vbox
label = Gtk.Label()
label.set_markup('Drag mouse over axes for position')
label.show()
vbox.pack_start(label, False, False, 0)
vbox.reorder_child(toolbar, -1)
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_gtk_sgskip.py:
.. only :: html
.. container:: sphx-glr-footer
:class: sphx-glr-footer-example
.. container:: sphx-glr-download sphx-glr-download-python
:download:`Download Python source code: pylab_with_gtk_sgskip.py `
.. container:: sphx-glr-download sphx-glr-download-jupyter
:download:`Download Jupyter notebook: pylab_with_gtk_sgskip.ipynb `
.. only:: html
.. rst-class:: sphx-glr-signature
Keywords: matplotlib code example, codex, python plot, pyplot
`Gallery generated by Sphinx-Gallery
`_