在jupyter笔记本中的plt.subplot

时间:2018-03-30 05:00:49

标签: python matplotlib jupyter-notebook

def plot_it(U1, U2, x, i):
    fig_1, = plt.plot(x, U1)
    fig_2, = plt.plot(x, U2)
    i = str(int(i/2 + 1)) if i != 0 else ''
    plt.xlabel('x, t =' + i + 'Δt')
    plt.ylabel('U')
    plt.legend(handles=[fig_1, fig_2], labels=['μ = 1', 'μ = 2'])
    plt.show()

plt.figure()
for i in range(11):
    U1[1:20] = np.linalg.solve(A1, B1.dot(U1[1:20]))
    U2[1:20] = np.linalg.solve(A2, B2.dot(U2[1:20]))
    if i % 2 == 0:
        plt.subplot(2, 3, int(i/2 + 1))
        plot_it(U1, U2, x, i)

我希望结果是2行3列,但实际上它给了我6行1列, enter image description here

1 个答案:

答案 0 :(得分:1)

我没有你的x,A1,A2等,所以我只是展示了如何使用子图 -

from matplotlib import pyplot as plt

x = [1,2,3,4,5,6,7]

fig, axes = plt.subplots(2, 3)

def plot_it(U1, U2, x, i, ax):
    ax.plot(x, U1)
    ax.plot(x, U2)
    i = str(int(i/2 + 1)) if i != 0 else ''
    fig_1 = ax.set_xlabel('x, t =' + i + 'dt')
    fig_2 = ax.set_ylabel('U')
    ax.legend(handles=[fig_1, fig_2], labels=['Test'])

plt.figure()
for i in range(11):
    U1 = [2,4,6,9,2,1, 2]
    U2 = [7, 9, 12, 78, 2, 12, 12]
    if i % 2 == 0:
        plot_it(U1, U2, x, i, axes[(i%4)/2, i/4])