How do I subclass matplotlib's figure class?

2019-02-15 20:13发布

I'm trying to add some custom behaviors and properties to my figures, but I'm having trouble deciding on an effective (and Pythonic) approach.

My first impulse is to simply subclass matplotlib.figure.Figure but I can't figure out how to accomplish this: I generally create new figures and start my plotting with something like

fig, ax = matplotlib.pyplot.subplots()

and I can't figure out how this would work with a custom Figure subclass.

How can I subclass Figure in a way that still lets me use pyplot methods like the one above? Is subclassing even the right way to get custom behaviors associated with a figure? Are the better approaches?

1条回答
Bombasti
2楼-- · 2019-02-15 21:01

subplots takes additional keyword arguments that are passed directly to figures(), one of which is the class used to create the figures.

class MyFigure(Figure):
    pass

fig, ax = matplotlib.pyplot.subplots(FigureClass=MyFigure)
查看更多
登录 后发表回答