【问题标题】:MatplotlibDeprecationWarning using GridspecMatplotlibDeprecationWarning 使用 Gridspec
【发布时间】:2020-08-09 23:31:41
【问题描述】:

我有一个函数可以返回我使用 subplot 和 gridspecs 创建的图形上的轴。它引发了一个 MatplotlibDeprecationWarning ,我想了解我应该如何做到这一点。有人可以帮忙吗?

def get_gridspec():
    fig10 = plt.figure(constrained_layout=True)
    gs0 = fig10.add_gridspec(1, 2)
    loss_gs = gs0[0].subgridspec(1, 1)
    graphs_gs = gs0[1].subgridspec(2, 1)
    fig10.add_subplot(loss_gs[0])

    for dataset in range(2):
        for irow_metric in range(2):
            for jrow_metric in range(1):
                fig10.add_subplot(graphs_gs[irow_metric, jrow_metric])
    return fig10.axes

加注:

MatplotlibDeprecationWarning: Adding an axes using the same arguments as a previous axes currently reuses the earlier instance.  In a future version, a new instance will always be created and returned.  Meanwhile, this warning can be suppressed, and the future behavior ensured, by passing a unique label to each axes instance.

fig10.add_subplot(graphs_gs[irow_metric, jrow_metric])

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    是的,这有点尴尬。我们正在研究一种不那么烦人的“子图”或“子面板”隐喻 (https://github.com/matplotlib/matplotlib/issues/17375)

    OTOH,从最近的 matplotlib 开始,gridspec 对象 (https://matplotlib.org/api/_as_gen/matplotlib.gridspec.GridSpecBase.html#matplotlib.gridspec.GridSpecBase.subplots) 上的 subplots 方法将在与 gridspec 关联的图形上创建子图:

    import matplotlib.pyplot as plt
    
    fig10 = plt.figure(constrained_layout=True)
    gs0 = fig10.add_gridspec(1, 2)
    loss_axs = gs0[0].subgridspec(1, 1).subplots()
    graphs_axs = gs0[1].subgridspec(2, 1).subplots()
    

    顺便说一句,弃用警告是因为您的外部循环 - 您“创建”了两次轴,这已被弃用。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-22
    • 1970-01-01
    • 2018-05-19
    • 1970-01-01
    • 1970-01-01
    • 2021-11-09
    • 2016-03-20
    • 1970-01-01
    相关资源
    最近更新 更多