【问题标题】:Get a list of all figures plotted using Matplotlib in Jupyter Notebook获取在 Jupyter Notebook 中使用 Matplotlib 绘制的所有图形的列表
【发布时间】:2022-01-17 13:03:59
【问题描述】:

我正在使用 Jupyter Notebook 进行数据分析,我需要在单独的单元格

中一个接一个地绘制多个数字

例如,假设我有 15 个数字,如下所示:

fig1 = plt.figure(...)
plt.barh(...)

...

fig2 = plt.figure(...)
plt.subplot(1,2,1)
plt.barh(...)

plt.subplot(1,2,2)
plt.barh(...)

...

fig15 = plt.figure(...) 
plt.barh(...)

如何获得这些人物名称的列表?像这样:

[fig1, fig2, ...., fig15]

P.S:我已经尝试过fig_nums = plt.get_fignums() - 它返回一个空列表:(

【问题讨论】:

    标签: python matplotlib jupyter-notebook


    【解决方案1】:

    plt.get_fignums() 没有返回任何内容,因为您没有创建图形,至少不是直接创建图形。 plt.barh() 返回一个容器。如果您想跟踪您的数据,请使用

    fig1 = plt.figure()
    ax1 = plt.barh(...)
    ...
    

    然后plt.get_fignums() 返回使用plt.figure() 创建的数字编号列表。

    如果这不是您想要的,最简单的方法是将所有 fig# 存储在 list

    figures = []
    fig1 = plt.barh(...)
    figures.append(fig1)
    ...
    ``
    

    【讨论】:

    • 感谢您的回复,我也在考虑ax 方法,但是在图形有子图的情况下如何使用它感到困惑(我已经更新了我的问题中的示例)
    • 您可以随时通过plt.figure()检索该图。因此,您可以构建一个类似figures = [plt.figure(i) for i in plt.get_fignums()] 的列表。 @Kunal
    【解决方案2】:

    我还发现了这种似乎对 jupyter 有帮助的解决方法:

    fig_list = [f for f in globals() if 'Figure' in str(type(globals()[f]))]
    print(fig_list)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-26
      • 1970-01-01
      • 2017-05-11
      相关资源
      最近更新 更多