【发布时间】:2023-03-11 14:04:01
【问题描述】:
我有一个包含所有数字列的 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 没有正确缩放。
我希望每个子图显示 10 个 bin,每个 bin 的宽度适合每个直方图。
有没有办法做到这一点,无论是调用df.plot 还是使用某种方法访问 Axes 对象?
【问题讨论】:
标签: python pandas matplotlib