You are reading an old version of the documentation (v2.2.0). For the latest version see https://matplotlib.org/stable/gallery/subplots_axes_and_figures/custom_figure_class.html
Version 2.2.0
matplotlib
Fork me on GitHub

Related Topics

This Page

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.

../../_images/sphx_glr_custom_figure_class_001.png
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()

Gallery generated by Sphinx-Gallery