【问题标题】:How to plot histogram with preaggregated data in Pandas?如何在 Pandas 中使用预聚合数据绘制直方图?
【发布时间】:2016-07-02 10:49:31
【问题描述】:

你能帮我找出如何用预先聚合的数据绘制直方图吗?我的意思是我已将数据分组到 bin 中以从 SQL Server 加载它们并保存到 xls 文件。现在我有两个变量:频率和 bin 变量(价格)。例如,我有一个 0 - 10 美元的价格箱。那里有120次出现。然后我有 10 - 20 美元的价格箱,那里有 500 次发生等等。

问题是我有太多的预聚合箱。因为价格从 0 变为 50 000,步长为 10。

我能否以某种方式在 pandas 中绘制一个直方图,它可以自动构建直方图并将每个观察结果视为不是一个单独的项目,而是已经预先计算了出现次数。

现在我有 322 个 bin 的直方图 - 我需要用 Python 将它们削减到 5 - 10:

【问题讨论】:

标签: python pandas matplotlib plot seaborn


【解决方案1】:

您可以根据您的数据绘制条形图(使用 matplotlib):

import matplotlib.pyplot as plt

n, bins = your_data()

binwidth = 0.8 * (bins[1] - bins[0])

# you might not need this, if your bins are already the centervalue
bincenter = (bins[:-1] + bins[1:]) / 2.

plt.bar(bincenter, n, align='center', width=binwidth)
plt.show()

【讨论】:

  • 感谢您的回答!我可能可以用条形图来做到这一点,但我需要减少垃圾箱的数量。我在 Excel 中发布了我现在拥有的情节。
猜你喜欢
  • 2015-01-23
  • 1970-01-01
  • 1970-01-01
  • 2015-01-05
  • 1970-01-01
  • 2012-08-11
  • 2021-12-21
  • 2018-11-09
  • 1970-01-01
相关资源
最近更新 更多