簡體   English   中英

如何從 Pandas 繪圖函數返回 matplotlib.figure.Figure 對象

[英]How to return a matplotlib.figure.Figure object from Pandas plot function

我有一個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

並使用 pandas 函數plot創建一個 barplot 對象

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

但我想要一個<matplotlib.figure.Figure object>而不是傳遞給其他函數,就像下面的對象類型:

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

那么如何將<matplotlib.axes.AxesSubplot object>轉換為<matplotlib.figure.Figure object>或直接從 Pandas plot 返回“Figure object”?

您可以從軸對象中獲取圖形:

ax.get_figure()

完整示例:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM