如何从 Pandas 绘图函数返回一个 matplotlib.figure.Figure 对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14936646/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 20:39:16  来源:igfitidea点击:

How to return a matplotlib.figure.Figure object from Pandas plot function?

matplotlibpandas

提问by bigbug

I have a dt:

我有一个dt

>>> dt
            sales                mg         
ID         600519   600809   600519   600809
RPT_Date                                    
20060331  13.5301   5.8951   9.4971   3.0408
20060630   6.6048   2.4081   4.3088   1.4040
20060930  12.3889   3.6053   9.1455   2.0754
20061231  16.5100   3.3659  12.4682   1.8810
20070331  15.8754   5.9129  11.5833   3.6736
20070630  10.4155   3.5759   7.8966   2.2812
20070930  18.2929   3.5280  14.3552   2.1584
20071231  27.7905   5.4510  23.7820   3.2568

And use pandas function plotto create a barplot object

并使用pandas函数plot创建一个barplot对象

>>> fig = dt.plot(kind='bar', use_index=True)
>>> fig
<matplotlib.axes.AxesSubplot object at 0x0B387150>

But I want a <matplotlib.figure.Figure object>intsead to pass to other function, just like the object type below:

但我想要一个<matplotlib.figure.Figure object>intsad 传递给其他函数,就像下面的对象类型:

>>> plt.figure()
<matplotlib.figure.Figure object at 0x123A6910>

So how can I transform <matplotlib.axes.AxesSubplot object>to <matplotlib.figure.Figure object>or directly return "Figure object" from Pandas plot ?

那么如何从 Pandas plot转换<matplotlib.axes.AxesSubplot object><matplotlib.figure.Figure object>或直接返回“Figure object”?

回答by joris

You can get the figure from the axes object:

您可以从轴对象中获取图形:

ax.get_figure()