使用Gridspec的MatplotlibDeprecationWarning

2024-09-27 09:35:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个函数,它返回我使用子地块和GridSpec创建的图形上的轴。它会引发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.

图10.添加子图(图[irow\u度量,jrow\u度量])

enter image description here


Tags: theinstanceingsaddforrangemetric
1条回答
网友
1楼 · 发布于 2024-09-27 09:35:48

是的,这有点尴尬。我们正在研究一个“子图形”或“子面板”的隐喻,它将不那么烦人(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()

顺便说一句,不推荐使用警告是因为您的外部循环-您正在“创建”轴两次,这是不推荐使用的

相关问题 更多 >

    热门问题