[英]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.