【问题标题】:Given a simple pandas Series, what's a simple way to create a histogram (bar plot) of it?给定一个简单的熊猫系列,创建直方图(条形图)的简单方法是什么?
【发布时间】:2018-05-12 10:33:23
【问题描述】:

我有一个这样的超简单系列:

hour
0     438
1     444
2     351
3     402
4     473
5     498
6     440
7     431
8     259
9      11
11     52
12     62
13     77
14     55
22     40
23    162
Name: value, dtype: int64

这只是在给定小时内对某事物的观察次数的计数。如何在 Jupyter 笔记本中快速轻松地将其绘制为直方图?第一个 bin 是从 0 到 1 小时(00:00 到 01:00),第二个 bin 是从 1 到 2 小时(01:00 到 02:00)等等。

【问题讨论】:

  • 你的意思是简单的条形图:ser.plot.bar()?
  • s.plot(kind='bar') ?
  • 看看matplotlib 或我个人最喜欢的seaborn

标签: python pandas numpy matplotlib histogram


【解决方案1】:

如果您需要标准条形图:

In [8]: import matplotlib
   ...: matplotlib.style.use('ggplot')
   ...:

In [9]: s.plot.bar(rot=0, grid=True, width=1, alpha=0.7)
Out[9]: <matplotlib.axes._subplots.AxesSubplot at 0xaaab7f0>

【讨论】:

  • 您使用的是什么版本的 pandas/matplotlib?我的默认 ggplot 样式总是看起来好像 color='r' 参数已通过。旁注,我总是在我的绘图中添加 edgecolor='k' 参数以在条之间添加一条黑线
  • @MattR,熊猫:0.21.0,matplotlib:2.1.0。您是否尝试“按原样”执行我的回答中的 sn-p?注意:旧版本的 Seaborn 在执行 import seaborn 后更改了设置,因此您可能需要关闭 ipython/Jupyter 并执行我的 sn -p not 首先导入 seaborn
  • 我实际上没有在这台机器上安装 seaborn(工作)。有趣的是风格仍然不同..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-09
  • 2016-04-25
  • 1970-01-01
相关资源
最近更新 更多