首页 > 解决方案 > 在 Pandas 中使用 Dataframe.plot 时如何更改每个子图的 bin 大小

问题描述

我有一个包含所有数字列的 DataFrame,其中列之间的数据范围差异很大。下面的代码提供了一个有代表性的例子:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({
    'A': np.random.randn(10000) * 20,
    'B': np.random.randn(10000) * 1000,
    'C': np.random.randn(10000) * 0.01,
    'D': np.random.randn(10000) * 300000,
    'E': np.random.randn(10000) * 500
})

axs = df.plot(kind = 'hist',subplots = True, bins = 10, layout = (2,3), figsize = (12,8), title = list(df.columns), sharex = False, sharey = True)

for i, ax in enumerate(axs.reshape(-1)):
    if i>= len(df.columns):
        break
    ax.set_xlim(df[df.columns[i]].min(),df[df.columns[i]].max())
    
plt.suptitle('Histograms for all features')
plt.tight_layout()
plt.show()

df.plot被调用时,xlim 范围被自动设置为具有最大数字的列的范围,这就是为什么我添加了for循环来解决这个问题。

但是,正如您在下面的屏幕截图中看到的那样,bin 没有正确缩放。

带有错误 bin 的直方图

我希望每个子图都显示 10 个 bin,每个 bin 的宽度适合每个直方图。有没有办法做到这一点,无论是调用df.plot还是使用某种方法访问 Axes 对象?

标签: pythonpandasmatplotlib

解决方案


您可以改用 pandas hist 函数

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({
    'A': np.random.randn(10000) * 20,
    'B': np.random.randn(10000) * 1000,
    'C': np.random.randn(10000) * 0.01,
    'D': np.random.randn(10000) * 300000,
    'E': np.random.randn(10000) * 500
})

df.describe()

plt.figure();
df.hist(bins = 10,layout = (2,3),density = True, figsize = (12,8), sharex = False, sharey = False
);


推荐阅读