带有分类索引DataFrame的pandas中的plt.plot问题

时间:2018-02-16 21:26:34

标签: python-3.x pandas matplotlib categorical-data

我有一个带分类索引的DataFrame,如下所示:

import pandas as pd
import matplotlib.pyplot as plt
%matplotlib notebook

accidents_by_day=pd.DataFrame({'num_accidents':[5659,5298,4917,4461,4181,4038,3985],
                           'weekday':[7,1,6,5,4,2,3]})

weekday_map={1:'Sunday',2:'Monday',3:'Tuesday',4:'Wednesday',5:'Thursday',6:'Friday',7:'Saturday'}
new_index=(pd.CategoricalIndex(accidents_by_day.weekday.map(weekday_map)).
       reorder_categories(new_categories=['Monday','Tuesday','Wednesday','Thursday',
                                          'Friday','Saturday','Sunday'],
                          ordered=True))
accidents_by_day.set_index(new_index,drop=True,inplace=True)
accidents_by_day.sort_index(inplace=True)

虽然以下工作正常:

accidents_by_day.num_accidents.plot(kind='bar')

plt.plot(accidents_by_day.num_accidents)发出错误

~\Anaconda3\lib\site-packages\matplotlib\axes\_base.py in _plot_args(self, tup, kwargs)
    390             func = self._makefill
    391 
--> 392         ncx, ncy = x.shape[1], y.shape[1]
    393         for j in xrange(max(ncx, ncy)):
    394             seg = func(x[:, j % ncx], y[:, j % ncy], kw, kwargs)

IndexError: tuple index out of range

plt.plot([accidents_by_day.num_accidents])产生一个空数字。

有人能解释一下这里发生了什么吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

plt.plot有两个参数,xyplt.plot(x,y)。如果您只指定一个参数plt.plot(y),则假定您要根据数字0, ..., len(y)-1进行绘图。那么这里可能的是绘制

plt.plot(accidents_by_day.num_accidents.values)

然而,生成的图可能不是所需的图,因为不考虑数据框索引。

所以你可以坚持通常的plt.plot(x,y)并提供索引,

plt.plot(accidents_by_day.index.categories, accidents_by_day.num_accidents.values)

enter image description here