【问题标题】:Customising Matplotlib Subplots自定义 Matplotlib 子图
【发布时间】:2021-03-23 19:07:24
【问题描述】:

我一直觉得 matplotlib 子图令人困惑,所以我想知道是否有人可以展示如何生成以下子图排列: Subplot Image

提前致谢。

【问题讨论】:

    标签: python matplotlib plot subplot


    【解决方案1】:

    您可以使用plt.GridSpec 轻松创建不同大小的子图。

    cols = 5   #number of columns in the grid
    s    = 2   #width of top right subplot in #cells
    w    = 1   #spacing between subplot columns
    
    fig = plt.figure()
    gs  = plt.GridSpec(2, cols, figure=fig, wspace=w)
    ax1 = fig.add_subplot(gs[0, :-s])
    ax2 = fig.add_subplot(gs[0, -s:])
    ax3 = fig.add_subplot(gs[1, :-s])
    

    【讨论】:

    • 注意你也可以用现代 matplotlib 做gs = fig.add_gridspec(2, cols)
    【解决方案2】:

    @Marc 的回答很好,但也许更简单、更灵活的是:

    fig, axs = plt.subplots(2, 2, gridspec_kw={'width_ratios':[2, 1]})
    # if you really don't want 4 axes:
    fig.delaxes(axs[1, 1])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-11
      • 2021-05-03
      • 2019-11-07
      • 2018-11-16
      • 2020-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多