繁体   English   中英

如何在 seaborn 中增加子图的大小

[英]How to increase size of a subplot in seaborn

我正在使用 seaborn 来绘制一些箱线图。 这是我的代码

fig, axs = plt.subplots(nrows = 8, ncols=2, figsize = (70,50))
plt.subplots_adjust(hspace = 0.8)

i = 0
j = 0

for item in items:
    ...
    # create dist as a list
    sns.boxplot(data = dist, ax = axs[i][j])

    j += 1

    if j == 2:
        i += 1
        j = 0

我正在改变 figsize() 的长度和宽度,但它不会改变我的子图的宽度? 这里有什么问题?

您还可以通过更改figsize来设置纵横比,因此在您的示例中使用类似figsize = (20, 80)而不是当前的(70, 50)可能会更好。

否则,您可能需要单独设置纵横比。 (例如,通过在for循环中添加axs[i][j].set_aspect(1)

您对plt.figure(figsize=(60,50))的调用正在创建一个与您在第一步中已经声明的 fig 不同的新空图。 在您对plt.subplots figsize 它在 plot 中默认为 (6,4),因为您没有设置它。 您已经创建了图形并分配给变量fig 如果您想对该数字采取行动,则应该fig.set_size_inches(12, 5)来更改大小。

然后,您可以简单地调用fig.tight_layout()以使 plot 安装得很好。

此外,还有一种更简单的方法可以通过在axes对象数组上使用flatten来迭代轴。 我也直接使用来自 seaborn 本身的数据。

# I first grab some data from seaborn and make an extra column so that there
# are exactly 8 columns for our 8 axes
data = sns.load_dataset('car_crashes')
data = data.drop('abbrev', axis=1)
data['total2'] = data['total'] * 2

# Set figsize here
fig, axes = plt.subplots(nrows=2, ncols=4, figsize=(12,5))

# if you didn't set the figsize above you can do the following
# fig.set_size_inches(12, 5)

# flatten axes for easy iterating
for i, ax in enumerate(axes.flatten()):
    sns.boxplot(x= data.iloc[:, i],  orient='v' , ax=ax)

fig.tight_layout()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM