如何子类化matplotlib的Figure类? [英] How do I subclass matplotlib's figure class?
问题描述
我试图在我的图形中添加一些自定义行为和属性,但是在确定有效(和Pythonic)方法时遇到了麻烦.
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.
我的第一个冲动就是简单地将matplotlib.figure.Figure
子类化,但是我不知道该怎么做:我通常会创建新的图形并以类似的方式开始绘图
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()
,我不知道如何使用自定义Figure
子类.
and I can't figure out how this would work with a custom Figure
subclass.
如何以一种仍然可以让我使用上述方法的pyplot
方法的方式来子类化Figure
?子类化是获得与图形关联的自定义行为的正确方法吗?是更好的方法吗?
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?
推荐答案
subplots
采用其他关键字参数,这些参数直接传递给figures()
,其中之一是用于创建图形的类.
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)
这篇关于如何子类化matplotlib的Figure类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!