Python - plt.subplot() 在 for 循环中创建绘图

Python - plt.subplot() with plot created in a for loop

我在 for 循环中创建了以下图:

cat_var={}
categories=['symboling','fuel-type','make']

for x in categories:
    cat_var[x]=df[x].unique()

for key in cat_var.keys():
    plt.figure(figsize=(10,10))
    for value in cat_var[key]:
        subset= df[df[key] == value]
        sns.distplot(subset['price'], hist = False, kde = True,kde_kws = {'linewidth': 3},label = value) 
    plt.legend(prop={'size': 16}, title = key)
    plt.title('Price distribution per %s level'% key)
    plt.xlabel('Price')
    plt.ylabel('Density')
    plt.show()

它正在工作,但我想绘制它们,使它们全部出现在同一行,而现在我将它们全部放在同一列。我正在考虑使用类似的东西:

fig, axes = plt.subplots(1, 3,figsize=(20,20))

但是无论我把这最后一行代码放在哪里,都不会产生所需的输出。

有什么想法吗?

如果你想在同一张图上有三个图,那么你需要使用子图并指定每个图的位置

fig, ax = plt.subplots(1, 3,figsize=(20,20))
for idx, key in enumerate(cat_var):       
    for value in cat_var[key]:
        subset= df[df[key] == value]
        sns.distplot(subset['price'], hist = False, ax= ax[idx], kde = True,kde_kws = {'linewidth': 3},label = value)  
plt.show()

具有以下输出: