【问题标题】:How to set individual sub-figure size in seaborn plot?如何在 seaborn 图中设置单个子图大小?
【发布时间】:2021-02-21 16:17:51
【问题描述】:

我有一个绘制多个图的代码 - 在 Seaborn 的单个图中绘制一个热图和一个条形图。但是,两个图的大小相同,即整体图的一半是热图,另一半是条形图。有没有办法控制单个绘图大小,使热图占据绘图大小的 75%,而条形图仅占绘图的 25%?

参考代码:

ig, ax = plt.subplots(1, 2, figsize=(7, 5))
heatmap = np.random.uniform(0, 1, size=(12, 12))
sns.heatmap(heatmap_scores, linewidth=0.5, cmap="OrRd", ax=ax[0])
ax[0].set_xlabel('Head')
ax[0].set_ylabel('Layer')
ax[0].set_title('Attention Heatmap')

x = np.mean(heatmap_scores, axis=1)
y = np.arange(0, 12)
sns.barplot(x=x, y=y, ax=ax[1], orient='h', color='r', dodge=False)
ax[1].set_title('Layer Average')
ax[1].set(yticklabels=[])

plt.savefig('fig.png')
plt.close()

【问题讨论】:

    标签: python python-3.x matplotlib seaborn


    【解决方案1】:

    您可以自定义GridSpec 选项,将它们传递给subplots,使用密钥gridspec_kw

    fig, ax = plt.subplots(1, 2, figsize=(7, 5), gridspec_kw={'width_ratios': [.75, .25]})
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-30
      • 1970-01-01
      • 2017-05-22
      • 2019-05-28
      • 2018-10-18
      • 1970-01-01
      • 2017-10-31
      • 2020-09-10
      相关资源
      最近更新 更多