自定义图类

您可以传递一个自定义的图构造函数,以确定是否要从默认图中派生。 这个简单的例子创建了一个带有图标题的图形。

自定义图类示例

  1. import matplotlib.pyplot as plt
  2. from matplotlib.figure import Figure
  3. class MyFigure(Figure):
  4. def __init__(self, *args, figtitle='hi mom', **kwargs):
  5. """
  6. custom kwarg figtitle is a figure title
  7. """
  8. super().__init__(*args, **kwargs)
  9. self.text(0.5, 0.95, figtitle, ha='center')
  10. fig = plt.figure(FigureClass=MyFigure, figtitle='my title')
  11. ax = fig.subplots()
  12. ax.plot([1, 2, 3])
  13. plt.show()

下载这个示例