重塑 pyplot 子图

Reshaping pyplot subplot

我有以下代码:

def plot_yz_over_x_elementwise(zarray):
    for i in range(zarray.shape[1]):
        for j in range(zarray.shape[2]):
            plt.subplot2grid((zarray.shape[1],zarray.shape[2]),(i,j))
            plt.plot(zarray[:,i,j])
            frame = plt.gca()
            frame.axes.get_xaxis().set_ticks([])
            frame.axes.get_yaxis().set_ticks([1,0.5,0])

它采用 3D np 数组并将其绘制成如下图形:

嵌入在 ipython 笔记本中。我需要的是某种方法来格式化此图,以便轴标签不会彼此重叠。我想这意味着要么在笔记本上展开它……要么在 python 中的每个单元格内缩小它。我该怎么做才能让这个情节看起来正确?

tight_layout 可能就是您要找的东西

def plot_yz_over_x_elementwise(zarray):
    for i in range(zarray.shape[1]):
        for j in range(zarray.shape[2]):
            plt.subplot2grid((zarray.shape[1],zarray.shape[2]),(i,j))
            plt.plot(zarray[:,i,j])
            frame = plt.gca()
            frame.axes.get_xaxis().set_ticks([])
            frame.axes.get_yaxis().set_ticks([1,0.5,0])
    plt.tight_layout()

很确定这是这个 question

的副本

底线答案是使用plt.tight_layout