如何用matplotlib在网格布局中创建多个子图

import matplotlib.pyplot as plt

fig = plt.figure(figsize=(12, 6))
fig.subplots_adjust(left=0.1, bottom=0.1, right=0.9, top=0.9, hspace=0.05, wspace=0.15)
#7行3列的网格
total_span = (7, 3)

for i, comp in enumerate(["E", "N", "Z"]):
    “”“
    loc=(x,y),colspan表示占的列数,rowspan表示占的行数
    每列会有 3 个子图,分别位于:
    顶部(第 0-1 行),
    中间(第 2-3 行),
    底部(第 5-6 行)。
    每个子图占用 1 列宽,2 行高。
    ”“”
    ax=plt.subplot2grid(shape=total_span, loc=(0, i), colspan=1, rowspan=2, fig=fig)
    ax.plot(X, Y, c="black", lw=0.5)

    plt.subplot2grid(shape=total_span, loc=(2, i), colspan=1, rowspan=2, fig=fig)
    ax.plot(X, Y, c="black", lw=0.5)

    plt.subplot2grid(shape=total_span, loc=(5, i), colspan=1, rowspan=2, fig=fig)
    ax.plot(X, Y, c="black", lw=0.5)

fig_file = "生成文件名.png"
gfig.savefig(fig_file, crop=True)
gfig.savefig(fig_file.replace(".png", ".pdf"), crop=False)

posted @ 2024-12-23 11:46  出门在外的旅行青蛙  阅读(31)  评论(0)    收藏  举报