【问题标题】:How to return a matplotlib.figure.Figure object from Pandas plot function如何从 Pandas 绘图函数返回 matplotlib.figure.Figure 对象
【发布时间】:2013-02-02 21:41:42
【问题描述】:

我有一个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创建条形图对象

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

但我想要一个 &lt;matplotlib.figure.Figure object&gt; 而不是传递给其他函数,就像下面的对象类型一样:

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

那么如何将&lt;matplotlib.axes.AxesSubplot object&gt; 转换为&lt;matplotlib.figure.Figure object&gt; 或直接从 Pandas 图中返回“图形对象”?

【问题讨论】:

    标签: python pandas matplotlib


    【解决方案1】:

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

    ax.get_figure()
    

    完整示例:

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

    【讨论】:

    • 我在函数的 return 语句中使用它来获取代表我的情节的对象
    猜你喜欢
    • 2017-03-27
    • 1970-01-01
    • 1970-01-01
    • 2015-12-15
    • 2020-05-06
    • 2012-02-29
    • 2019-10-13
    • 1970-01-01
    • 2021-01-30
    相关资源
    最近更新 更多