【问题标题】:Adjust hspace one-sided for matplotlib subplots为 matplotlib 子图单面调整 hspace
【发布时间】:2021-05-31 06:21:14
【问题描述】:

我的问题是基于这个问题: Adjust hspace for some of the subplots

调整多个子图的顶部图并增加 hspace 的差异。我想增加子图中两个图之间的 hspace(在我的情况下:从顶部开始的 plot 3 和 plot4 之间)。

这是我的例子:

import numpy as np
import matplotlib.pyplot as plt

noise = np.random.rand(300)


gs_top = plt.GridSpec(9, 1, hspace=0.5)
gs_base = plt.GridSpec(9, 1, hspace=0)


fig = plt.figure()
fig.patch.set_facecolor('white')

ax0 = fig.add_subplot(gs_base[0,:])
ax1 = fig.add_subplot(gs_base[1,:])
ax2 = fig.add_subplot(gs_top[2,:])

ax3 = fig.add_subplot(gs_base[3,:])
ax4 = fig.add_subplot(gs_base[4,:])
ax5 = fig.add_subplot(gs_base[5,:])


ax0.plot(noise)
ax1.plot(noise)
ax2.plot(noise)
ax3.plot(noise)
ax4.plot(noise)
ax5.plot(noise)

在示例中,显示图 3 和图 4 之间的 hspace 增加。但是,我不想增加图 2 和图 3 之间的空间。 如何仅在一侧调整 hspace 变量?

【问题讨论】:

    标签: python-3.x matplotlib


    【解决方案1】:

    通过使用各种单词组合询问操纵 google 后找到了答案。找到这个:Stackoverflow answer

    简而言之(肮脏的方式): 添加一个单独的轴并使其不可见。 示例:

    import numpy as np
    import matplotlib.pyplot as plt
    
    noise = np.random.rand(300)
    
    
    gs_base = plt.GridSpec(7, 1, hspace=0, height_ratios=[1, 1, 1, 0.8, 1,1,1])
    
    
    fig = plt.figure()
    fig.patch.set_facecolor('white')
    
    ax0 = fig.add_subplot(gs_base[0,:])
    ax1 = fig.add_subplot(gs_base[1,:])
    ax2 = fig.add_subplot(gs_base[2,:])
    
    ax3 = fig.add_subplot(gs_base[3,:])
    ax3.set_visible(False)
    ax4 = fig.add_subplot(gs_base[4,:])
    ax5 = fig.add_subplot(gs_base[5,:])
    ax6 = fig.add_subplot(gs_base[6,:])
    
    
    ax0.plot(noise)
    ax1.plot(noise)
    ax2.plot(noise)
    ax4.plot(noise)
    ax5.plot(noise)
    ax6.plot(noise)
    

    长篇大论(正确的方式):

    暂时想不通。

    【讨论】:

    猜你喜欢
    • 2019-03-10
    • 2015-05-03
    • 2013-09-15
    • 1970-01-01
    • 2021-06-27
    • 1970-01-01
    • 2021-09-13
    • 2020-08-14
    • 2011-01-25
    相关资源
    最近更新 更多