用seaborn绘制多个直方图

时间:2018-11-06 12:01:22

标签: python-3.x pandas plot histogram seaborn

我有一个36列的数据框。我想使用seaborn一口气(6x6)绘制每个功能的直方图。基本复制df.hist()但带有重生性。下面的代码仅显示第一个功能的图,其他所有功能都为空。

enter image description here

测试数据框:

df = pd.DataFrame(np.random.randint(0,100,size=(100, 36)), columns=range(0,36))

我的代码:

import seaborn as sns
# plot
f, axes = plt.subplots(6, 6, figsize=(20, 20), sharex=True)
for feature in df.columns:
    sns.distplot(df[feature] , color="skyblue", ax=axes[0, 0])

1 个答案:

答案 0 :(得分:1)

我想同时循环遍历轴和特征是很有意义的。

f, axes = plt.subplots(6, 6, figsize=(20, 20), sharex=True)
for ax, feature in zip(axes.flat, df.columns):
    sns.distplot(df[feature] , color="skyblue", ax=ax)

Numpy数组按行进行展平,即您将在第一行中获得前6个要素,在第二行中获得6至11个要素,等等。

如果这不是您想要的,则可以手动为轴数组定义索引,

f, axes = plt.subplots(6, 6, figsize=(20, 20), sharex=True)
    for i, feature in enumerate(df.columns):
        sns.distplot(df[feature] , color="skyblue", ax=axes[i%6, i//6])

例如以上将逐列填充子图。

相关问题