python - Pandas :带有 multiIndex 数据框的条形图

标签 python pandas matplotlib

我有一个带有 TIMESTAMP 列(不是索引)的 pandas DataFrame,时间戳格式如下:

2015-03-31 22:56:45.510

我还有名为 CLASSAXLES 的列。我想为 AXLES 的每个唯一值分别计算每个月的记录数(AXLES 可以取 3- 12).

我想到了 resamplegroupby 的组合:

resamp = dfWIM.set_index('TIMESTAMP').groupby('AXLES').resample('M', how='count').CLASS

这似乎给了我一个 multiIndex 数据框对象,如下所示。

In [72]: resamp

Out [72]:

AXLES  TIMESTAMP 
3      2014-07-31      5517
       2014-08-31     31553
       2014-09-30     42816
       2014-10-31     49308
       2014-11-30     44168
       2014-12-31     45518
       2015-01-31     54782
       2015-02-28     52166
       2015-03-31     47929
4      2014-07-31      3147
       2014-08-31     24810
       2014-09-30     39075
       2014-10-31     46857
       2014-11-30     42651
       2014-12-31     48282
       2015-01-31     42708
       2015-02-28     43904
       2015-03-31     50033

从这里,我如何访问这个 multiIndex 对象的不同组件来为以下条件创建条形图?

  • 显示 AXLES = 3 时的数据
  • 以月 - 年格式显示 x 个刻度(没有天、小时、分钟等)

谢谢!

编辑:以下代码给出了情节,但我无法将 xtick 格式更改为 MM-YY。

resamp[3].plot(kind='bar')

enter image description here

EDIT 2 下面是一个代码片段,它生成了一个类似于我所拥有的数据的小样本:

dftest = {'TIMESTAMP':['2014-08-31','2014-09-30','2014-10-31'], 'AXLES':[3, 3, 3], 'CLASS':[5,6,7]}
dfTest = pd.DataFrame(dftest)
dfTest.TIMESTAMP = pd.to_datetime(pd.Series(dfTest.TIMESTAMP))
resamp = dfTest.set_index('TIMESTAMP').groupby('AXLES').resample('M', how='count').CLASS
resamp[3].plot(kind='bar')

编辑 3: 下面是解决方案:

A.绘制整个重采样数据框(基于@Ako 的建议):

df = resamp.unstack(0)
df.index = [ts.strftime('%b 20%y') for ts in df.index]
df.plot(kind='bar', rot=0)

enter image description here

B.从重新采样的数据框中绘制一个单独的索引(基于@Alexander 的建议):

df = resamp[3]
df.index = [ts.strftime('%b 20%y') for ts in df.index]
df.plot(kind='bar', rot=0)

enter image description here

最佳答案

您可以使用 ax.xaxis.set_major_formatter 显式生成和设置标签用ticker.FixedFormatter .这将允许您保留带有时间戳值的 DataFrame 的 MultiIndex,同时显示所需的 %m-%Y 中的时间戳。格式:

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

dftest = {'TIMESTAMP':['2014-08-31','2014-09-30','2014-10-31'], 'AXLES':[3, 3, 3], 'CLASS':[5,6,7]}
dfTest = pd.DataFrame(dftest)
dfTest.TIMESTAMP = pd.to_datetime(pd.Series(dfTest.TIMESTAMP))
resamp = dfTest.set_index('TIMESTAMP').groupby('AXLES').resample('M', how='count').CLASS

ax = resamp[3].plot(kind='bar')
ticklabels = [timestamp.strftime('%m-%Y') for axle, timestamp in resamp.index]
ax.xaxis.set_major_formatter(ticker.FuncFormatter(lambda x, pos: ticklabels[int(x)]))
plt.gcf().autofmt_xdate()

plt.show()

产量 enter image description here

关于python - Pandas :带有 multiIndex 数据框的条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33642388/

相关文章:

python - 使用 PyPlot 绘制平滑线

python - 无法重命名由 .sum() 函数创建的数据框的列名称

python - 如何配置 Qt4Agg 后端的行为?

python - 如何迭代 Pandas 数据框,并应用阈值函数来删除 x% 为空的列?

java - Jython 没有名为 random 的模块

python - 使用 Pandas 读取数据(.dat 文件)

python - 使用现有 TimeSerie 中的索引和另一个 TimeSerie 中的列在 Pandas 中创建 DataFrame

python - 对按列值映射的背景单元格颜色进行着色

Python - 将 argparse 变量传递给类

python - 更改 Tensorflow 中新分配变量的形状