首页 > 解决方案 > 将数据框中的所有列绘制到单个图 Python

问题描述

我有一个包含 x 列的数据框(x 因用户的输入而异)。我希望能够将每个数据帧中的所有列绘制为单个直方图。

到目前为止,此循环的一些代码如下所示:

for var_name in variables:

    df_var = (rand_sample.filter(like=str(var_name))) # filter on each variable for comparison
    print(df_var)

    # Plot Frequency
    fig = plt.subplots(figsize=(12, 8))
    plt.hist([df_var], alpha=0.35, bins=bin_size)
    plt.title(str(j), fontsize=18)
    plt.xlabel('Porosity Density', fontsize=15)
    plt.ylabel('Frequency', fontsize=15)
    plt.grid(True)
    plt.show()

variables以前用想要的字符串制作的列表在哪里,我正在过滤以组合在一起df_var

我得到错误:

ValueError: cannot copy sequence with size 107574 to array axis with dimension 2

这意味着2 columns试图在1 column这里绘制 - 我明白这一点;我只是不知道如何编写代码来plt.hist([df_var]...)为任意数量的列工作。

标签: pythonpython-3.xpandasmatplotlib

解决方案


推荐阅读