Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
414 views
in Technique[技术] by (71.8m points)

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

I have a 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 plot to create a barplot object

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

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

>>> 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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can get the figure from the axes object:

ax.get_figure()

Full example:

df = pd.DataFrame({'a': range(10)})
ax = df.plot.barh()
ax.get_figure()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...