{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Embed in GTK4\n\nDemonstrate adding a FigureCanvasGTK4Agg widget to a Gtk.ScrolledWindow using\nGTK4 accessed via pygobject.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import gi\n\ngi.require_version('Gtk', '4.0')\nfrom gi.repository import Gtk\n\nimport numpy as np\n\nfrom matplotlib.backends.backend_gtk4agg import FigureCanvasGTK4Agg as FigureCanvas\nfrom matplotlib.figure import Figure\n\n\ndef on_activate(app):\n    win = Gtk.ApplicationWindow(application=app)\n    win.set_default_size(400, 300)\n    win.set_title(\"Embedded in GTK4\")\n\n    fig = Figure(figsize=(5, 4), dpi=100)\n    ax = fig.add_subplot()\n    t = np.arange(0.0, 3.0, 0.01)\n    s = np.sin(2*np.pi*t)\n    ax.plot(t, s)\n\n    # A scrolled margin goes outside the scrollbars and viewport.\n    sw = Gtk.ScrolledWindow(margin_top=10, margin_bottom=10,\n                            margin_start=10, margin_end=10)\n    win.set_child(sw)\n\n    canvas = FigureCanvas(fig)  # a Gtk.DrawingArea\n    canvas.set_size_request(800, 600)\n    sw.set_child(canvas)\n\n    win.present()\n\n\napp = Gtk.Application(application_id='org.matplotlib.examples.EmbeddingInGTK4')\napp.connect('activate', on_activate)\napp.run(None)"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.14.6"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}