具有类别索引DataFrame的 pandas 的plt.plot问题 [英] plt.plot issue in pandas with categorical index DataFrame
问题描述
我有一个具有分类索引的DataFrame,如下所示:
将熊猫导入为pd
导入matplotlib.pyplot为plt
%matplotlib笔记本
意外_by_day = pd.DataFrame({'num_accidents':[5659,5298,4917,4461,4181,4038,3985],
'工作日' :[7,1,6,5,4,2,3]})
weekday_map = {1:'Sunday',2:'Monday',3:'Tuesday',4:'星期三',5:星期四,6:星期五,7:星期六}
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)
意外_by_day.sort_index(inplace = True)
以下代码可以正常工作:
accidents_by_day.num_accidents.plot(kind ='bar')
I have a DataFrame with categorical index like so:
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)
While The following works fine:
accidents_by_day.num_accidents.plot(kind='bar')
The plt.plot(accidents_by_day.num_accidents)
gives an error
~\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
and plt.plot([accidents_by_day.num_accidents])
produces an empty figure.
Could anyone explain what is happening here?
Thanks!
plt.plot
takes two arguments, x
and y
: plt.plot(x,y)
. If you only specify a single argument, plt.plot(y)
, it is assumed that you want to plot against the numbers 0, ..., len(y)-1
. So what is possible here, is to plot
plt.plot(accidents_by_day.num_accidents.values)
The produced plot might however not be the desired one, since the dataframe index is not taken into account.
So you may stick to the usual plt.plot(x,y)
and supply the index,
plt.plot(accidents_by_day.index.categories, accidents_by_day.num_accidents.values)
这篇关于具有类别索引DataFrame的 pandas 的plt.plot问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!