代码之家  ›  专栏  ›  技术社区  ›  Wolpertinger

清除Matplotlib中的子时隙

  •  30
  • Wolpertinger  · 技术社区  · 8 年前

    我在一个图中有许多子图 fig1 ,通过创建

    ax = fig1.add_subplot(221)
    

    然后我在每个子图中通过

    im=ax.plot(x,y)
    

    并通过添加一些轴标签

    ax.set_xlabel('xlabel')
    

    然后,我想完全清除一个子图,如中所述 When to use cla(), clf() or close() for clearing a plot in matplotlib? . 然而问题是 ax.cla() ax.clear() 另一方面,似乎只清除绘图中的数据,而不删除轴、轴刻度标签等 plt.clf()

    1 回复  |  直到 8 年前
        1
  •  53
  •   ImportanceOfBeingErnest    8 年前
    • ax.clear() 清除轴。也就是说,它删除了轴上的所有设置和数据,这样您就只剩下一个轴,就像刚刚创建的一样。

    • ax.axis("off") 关闭轴,以便隐藏所有轴的脊椎和标签。

    • ax.set_visible(False) 使整个轴不可见,包括其中的数据。

    • ax.remove() 从图形中删除轴。

    完整示例:

    import matplotlib.pyplot as plt
    
    fig,axes = plt.subplots(2,3)
    for ax in axes.flat:
        ax.plot([2,3,1])
    
    axes[0,1].clear()
    axes[1,0].axis("off")
    axes[1,1].set_visible(False)
    axes[0,2].remove()
    
    plt.show()
    

    enter image description here