创建for循环以使用Seaborn绘制DataFrame的各个列的直方图

Tho*_*ick 2 python plot pandas seaborn

因此,我试图使用for循环为DatFrame中的所有连续变量绘制直方图,我已经设法使用countplot通过以下代码为分类变量完成此操作:

df1 = df.select_dtypes([np.object])

for i, col in enumerate(df1.columns):
    plt.figure(i)
    sns.countplot(x=col, data=df1)
Run Code Online (Sandbox Code Playgroud)

我在这里通过搜索找到了。

但是现在我想对distplot做同样的事情,所以我尝试将上面的代码修改为:

df1 = dftest.select_dtypes([np.int, np.float])

for i, col in enumerate(df1.columns):
    plt.figure(i)
    sns.distplot(df1)
Run Code Online (Sandbox Code Playgroud)

但这只是给我一个空想。关于我能做什么的任何想法?

编辑:例如,DataFrame:

dftest = pd.DataFrame(np.random.randint(low=0, high=10, size=(5, 5)),
                    columns=['a', 'b', 'c', 'd', 'e']) 
Run Code Online (Sandbox Code Playgroud)

Imp*_*est 6

似乎您想用distplot数据帧的每一列生成一个图形。因此,您需要指定每个特定图形使用的数据。

正如seaborn文档所说的那样distplot(a, ...)

a:系列,一维数组或列表。观测数据。

因此,在这种情况下:

for i, col in enumerate(df1.columns):
    plt.figure(i)
    sns.distplot(df1[col])
Run Code Online (Sandbox Code Playgroud)


val*_*e55 5

定义一个函数来绘制直方图

def histograms_plot(数据框,特征,行,列):

fig=plt.figure(figsize=(20,20))
for i, feature in enumerate(features):
    ax=fig.add_subplot(rows,cols,i+1)
    dataframe[feature].hist(bins=20,ax=ax,facecolor='green')
    ax.set_title(feature+" Distribution",color='red')

fig.tight_layout()  
plt.show()
Run Code Online (Sandbox Code Playgroud)

histograms_plot(df,df.columns,6,3)