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

标签 python pandas plot 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)

这是我通过搜索 SO 找到的。

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

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

for i, col in enumerate(df1.columns):
    plt.figure(i)
    sns.distplot(df1)

但它只给了我一个空的情节。关于我可以做什么的任何想法?

编辑:例如DataFrame:

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

最佳答案

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

作为seaborn documentation对于 distplot(a, ...)

a : Series, 1d-array, or list. Observed data.

所以在这种情况下:

for i, col in enumerate(df1.columns):
    plt.figure(i)
    sns.distplot(df1[col])

关于python - 创建 for 循环以使用 Seaborn 为 DataFrame 的各个列绘制直方图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50773877/

相关文章:

python - Pandas 相对时间轴

python - 缩放后 matshow/imshow 图周围的额外空间

r - 在 ggplot2 中绘制季节性时间序列

python - 为什么某些代码在 Python2 中是确定性的,而在 Python 3 中是非确定性的?

python - Flask 的子域?

python - Pandas - 为所有行(特别是)重复行提供唯一标识符

python - 如何统计数据框中每个值出现的频率?

matlab - 为堆叠条形图中的每个部分设置不同的颜色

python - Django,可以运行两个不同的版本吗?

python - 是否可以在每 x 行被解释的行中调用一个函数?