d13*_*337 45 python matplotlib pandas
我想通过让Y轴显示整个数据集大小中每列的百分比而不是绝对值来比较两个直方图.那可能吗?我正在使用Pandas和matplotlib.谢谢
Rut*_*ies 56
在density=True(normed=True为matplotlib < 2.2.0)返回其直方图np.sum(pdf * np.diff(bins))如果你想直方图的总和为1,您可以使用numpy的的直方图()和自己正常化的结果等于1.
x = np.random.randn(30)
fig, ax = plt.subplots(1,2, figsize=(10,4))
ax[0].hist(x, density=True, color='grey')
hist, bins = np.histogram(x)
ax[1].bar(bins[:-1], hist.astype(np.float32) / hist.sum(), width=(bins[1]-bins[0]), color='grey')
ax[0].set_title('normed=True')
ax[1].set_title('hist = hist / hist.sum()')
Run Code Online (Sandbox Code Playgroud)

顺便说一句:奇怪的是在左图的第一个区域绘制小故障.
小智 15
我知道这个答案是 6 年后,但对于任何使用 density=True(normed=True 的替代品)的人来说,这并不是您想要的。它将对整个分布进行归一化,以便 bin 的面积为 1。因此,如果您有更多宽度 < 1 的 bin,则可以预期高度 > 1(y 轴)。如果要将直方图绑定到 [0;1],则必须自己计算。
小智 14
Pandas绘图可以接受来自相应matplotlib函数的任何额外关键字参数.因此,为了完整性来自其他人的评论,这就是人们如何做到的:
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(100,2), columns=list('AB'))
df.hist(density=1)
Run Code Online (Sandbox Code Playgroud)
此外,为了直接比较,这也可能是一个好方法:
df.plot(kind='hist', density=1, bins=20, stacked=False, alpha=.5)
Run Code Online (Sandbox Code Playgroud)
hob*_*obs 13
看起来像@CarstenKönig 找到了正确的方法:
df.hist(bins=20, weights=np.ones_like(df[df.columns[0]]) * 100. / len(df))
Run Code Online (Sandbox Code Playgroud)
您可以使用np.ones_like()简化加权:
df["ColumnName"].plot.hist(weights = np.ones_like(df.index) / len(df.index))
Run Code Online (Sandbox Code Playgroud)
I see this is an old question but it shows up on top for some searches, so I think as of 2021 seaborn would be an easy way to do this.
You can do something like this:
import seaborn as sns
sns.histplot(df,stat="probability")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
45189 次 |
| 最近记录: |