将数据框中的所有列绘制到单个图形Python

时间:2019-02-15 21:49:38

标签: python python-3.x pandas matplotlib

我有一个数据框,其中有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]...)适用于任意数量的列。

0 个答案:

没有答案