.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/subplots_axes_and_figures/custom_figure_class.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_subplots_axes_and_figures_custom_figure_class.py: ======================== Custom Figure subclasses ======================== You can pass a `.Figure` subclass to `.pyplot.figure` if you want to change the default behavior of the figure. This example defines a `.Figure` subclass ``WatermarkFigure`` that accepts an additional parameter ``watermark`` to display a custom watermark text. The figure is created using the ``FigureClass`` parameter of `.pyplot.figure`. The additional ``watermark`` parameter is passed on to the subclass constructor. .. GENERATED FROM PYTHON SOURCE LINES 15-43 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from matplotlib.figure import Figure class WatermarkFigure(Figure): """A figure with a text watermark.""" def __init__(self, *args, watermark=None, **kwargs): super().__init__(*args, **kwargs) if watermark is not None: bbox = dict(boxstyle='square', lw=3, ec='gray', fc=(0.9, 0.9, .9, .5), alpha=0.5) self.text(0.5, 0.5, watermark, ha='center', va='center', rotation=30, fontsize=40, color='gray', alpha=0.5, bbox=bbox) x = np.linspace(-3, 3, 201) y = np.tanh(x) + 0.1 * np.cos(5 * x) plt.figure(FigureClass=WatermarkFigure, watermark='draft') plt.plot(x, y) .. image-sg:: /gallery/subplots_axes_and_figures/images/sphx_glr_custom_figure_class_001.png :alt: custom figure class :srcset: /gallery/subplots_axes_and_figures/images/sphx_glr_custom_figure_class_001.png, /gallery/subplots_axes_and_figures/images/sphx_glr_custom_figure_class_001_2_00x.png 2.00x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 44-52 .. admonition:: References The use of the following functions, methods, classes and modules is shown in this example: - `matplotlib.pyplot.figure` - `matplotlib.figure.Figure` - `matplotlib.figure.Figure.text` .. _sphx_glr_download_gallery_subplots_axes_and_figures_custom_figure_class.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: custom_figure_class.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: custom_figure_class.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_