.. note::
    :class: sphx-glr-download-link-note

    Click :ref:`here <sphx_glr_download_gallery_subplots_axes_and_figures_custom_figure_class.py>` to download the full example code
.. rst-class:: sphx-glr-example-title

.. _sphx_glr_gallery_subplots_axes_and_figures_custom_figure_class.py:


===================
Custom Figure Class
===================

You can pass a custom Figure constructor to figure if you want to derive from
the default Figure.  This simple example creates a figure with a figure title.




.. image:: /gallery/subplots_axes_and_figures/images/sphx_glr_custom_figure_class_001.png
    :class: sphx-glr-single-img





.. code-block:: python


    import matplotlib.pyplot as plt
    from matplotlib.figure import Figure


    class MyFigure(Figure):
        def __init__(self, *args, **kwargs):
            """
            custom kwarg figtitle is a figure title
            """
            figtitle = kwargs.pop('figtitle', 'hi mom')
            Figure.__init__(self, *args, **kwargs)
            self.text(0.5, 0.95, figtitle, ha='center')


    fig = plt.figure(FigureClass=MyFigure, figtitle='my title')
    ax = fig.subplots()
    ax.plot([1, 2, 3])

    plt.show()


.. _sphx_glr_download_gallery_subplots_axes_and_figures_custom_figure_class.py:


.. only :: html

 .. container:: sphx-glr-footer
    :class: sphx-glr-footer-example



  .. container:: sphx-glr-download

     :download:`Download Python source code: custom_figure_class.py <custom_figure_class.py>`



  .. container:: sphx-glr-download

     :download:`Download Jupyter notebook: custom_figure_class.ipynb <custom_figure_class.ipynb>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    Keywords: matplotlib code example, codex, python plot, pyplot
    `Gallery generated by Sphinx-Gallery
    <https://sphinx-gallery.readthedocs.io>`_
