python - jupyter笔记本中的plt.subplot

标签 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

最佳答案

我没有你的 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])

关于python - jupyter笔记本中的plt.subplot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49568625/

相关文章:

python - 如何将代码片段保存到数据库?

python - matplotlib 中未显示绘图窗口

python - 如何为按时间排序的 matplotlib 图序列制作动画

python - Arcgis Pro 2.0 Jupyter Notebook 安装失败

python - 如何通过索引添加两个字符串

python - 什么时候 "i += x"与 Python 中的 "i = i + x"不同?

python - tensorflow 。将一列数组 (200,) 放入一列占位符 [无,1] 会产生形状错误

python - 如何在(seaborn)KDE图中找到中位数?

python - 没有名为 'mlxtend' 的模块

python - 无法导入jupyter笔记本中已安装的包