Python 2.7 如何在Dataframe中按天分组箱线图

Python 2.7 如何在Dataframe中按天分组箱线图,python-2.7,pandas,matplotlib,boxplot,datetimeindex,Python 2.7,Pandas,Matplotlib,Boxplot,Datetimeindex,我有如下数据: Observed WRF 2014-06-28 12:00:00 0.000000 1.823554 2014-06-28 13:00:00 0.000000 1.001567 2014-06-28 14:00:00 0.000000 0.309840 2014-06-28 15:00:00 0.000000 0.889811 2014-06-28 16:00:00 0.000000 0.939780 20

我有如下数据:

                     Observed       WRF
2014-06-28 12:00:00  0.000000  1.823554
2014-06-28 13:00:00  0.000000  1.001567
2014-06-28 14:00:00  0.000000  0.309840
2014-06-28 15:00:00  0.000000  0.889811
2014-06-28 16:00:00  0.000000  0.939780
2014-06-28 17:00:00  1.251794  1.271781
2014-06-28 18:00:00  1.610596  0.935092
2014-06-28 19:00:00  2.129068  0.868775
2014-06-28 20:00:00  2.326501  0.892550
...
2014-08-31 05:00:00  0.365868  2.463277
2014-08-31 06:00:00  0.281729  1.233760
2014-08-31 07:00:00  0.197590  0.427411
2014-08-31 08:00:00  0.127754  0.299558
2014-08-31 09:00:00  0.000000  0.571106
2014-08-31 10:00:00  0.000000  0.449634
2014-08-31 11:00:00  0.000000  0.324269
2014-08-31 12:00:00  0.000000  1.725650
我希望生成一个图表,上面有两组不同颜色的箱线图。现在,我一开始并不擅长绘制箱线图,所以我的技术可能会失败。我已生成以下代码:

df7.boxplot(by='day',whis=[10,90],sym=' ',figsize=(16,8),color=((1,0.502,0),'black'))\
.legend(loc='lower center', bbox_to_anchor=(1.007, -0.06),prop={'size':16})
plt.subplots_adjust(left=.1, right=0.9, top=0.9, bottom=.2)
plt.title('Five Day WRF Model Comparison Near %.2f,%.2f' %(lat,lon),fontsize=24)
plt.ylabel('Hourly Wind Speed [$W/m^2$]',fontsize=18,color='black')
ax7=plt.gca()
ax7.xaxis.set_label_coords(0.5, -0.05) 
plt.xlabel('Time',fontsize=18,color='black')
plt.show()
这就给了我:

File "<ipython-input-35-9945f2efb84e>", line 1, in <module>
    df7.boxplot(by='D',whis=[10,90],sym=' ',figsize=(16,8),color=((1,0.502,0),'black')).legend(loc='lower center', bbox_to_anchor=(1.007, -0.06),prop={'size':16})

  File "...\Anaconda2\lib\site-packages\pandas\core\frame.py", line 5581, in boxplot
    return_type=return_type, **kwds)

  File "...\Anaconda2\lib\site-packages\pandas\tools\plotting.py", line 2747, in boxplot
    return_type=return_type)

  File "...\Anaconda2\lib\site-packages\pandas\tools\plotting.py", line 3139, in _grouped_plot_by_column
    grouped = data.groupby(by)

  File "...\Anaconda2\lib\site-packages\pandas\core\generic.py", line 3778, in groupby
    **kwargs)

  File "...\Anaconda2\lib\site-packages\pandas\core\groupby.py", line 1427, in groupby
    return klass(obj, by, **kwds)

  File "...\Anaconda2\lib\site-packages\pandas\core\groupby.py", line 354, in __init__
    mutated=self.mutated)

  File "...\Anaconda2\lib\site-packages\pandas\core\groupby.py", line 2383, in _get_grouper
    in_axis, name, gpr = True, gpr, obj[gpr]

  File "...\Anaconda2\lib\site-packages\pandas\core\frame.py", line 1997, in __getitem__
    return self._getitem_column(key)


  File "...\Anaconda2\lib\site-packages\pandas\core\frame.py", line 2004, in _getitem_column
    return self._get_item_cache(key)

  File "...\Anaconda2\lib\site-packages\pandas\core\generic.py", line 1350, in _get_item_cache
    values = self._data.get(item)

  File "...\Anaconda2\lib\site-packages\pandas\core\internals.py", line 3290, in get
    loc = self.items.get_loc(item)

  File "...\Anaconda2\lib\site-packages\pandas\indexes\base.py", line 1947, in get_loc
    return self._engine.get_loc(self._maybe_cast_indexer(key))

  File "pandas\index.pyx", line 137, in pandas.index.IndexEngine.get_loc (pandas\index.c:4154)

  File "pandas\index.pyx", line 159, in pandas.index.IndexEngine.get_loc (pandas\index.c:4018)

  File "pandas\hashtable.pyx", line 675, in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:12368)

  File "pandas\hashtable.pyx", line 683, in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:12322)

KeyError: 'D'
AssertionError: Grouper and axis must be same length
这就给了我:

File "<ipython-input-35-9945f2efb84e>", line 1, in <module>
    df7.boxplot(by='D',whis=[10,90],sym=' ',figsize=(16,8),color=((1,0.502,0),'black')).legend(loc='lower center', bbox_to_anchor=(1.007, -0.06),prop={'size':16})

  File "...\Anaconda2\lib\site-packages\pandas\core\frame.py", line 5581, in boxplot
    return_type=return_type, **kwds)

  File "...\Anaconda2\lib\site-packages\pandas\tools\plotting.py", line 2747, in boxplot
    return_type=return_type)

  File "...\Anaconda2\lib\site-packages\pandas\tools\plotting.py", line 3139, in _grouped_plot_by_column
    grouped = data.groupby(by)

  File "...\Anaconda2\lib\site-packages\pandas\core\generic.py", line 3778, in groupby
    **kwargs)

  File "...\Anaconda2\lib\site-packages\pandas\core\groupby.py", line 1427, in groupby
    return klass(obj, by, **kwds)

  File "...\Anaconda2\lib\site-packages\pandas\core\groupby.py", line 354, in __init__
    mutated=self.mutated)

  File "...\Anaconda2\lib\site-packages\pandas\core\groupby.py", line 2383, in _get_grouper
    in_axis, name, gpr = True, gpr, obj[gpr]

  File "...\Anaconda2\lib\site-packages\pandas\core\frame.py", line 1997, in __getitem__
    return self._getitem_column(key)


  File "...\Anaconda2\lib\site-packages\pandas\core\frame.py", line 2004, in _getitem_column
    return self._get_item_cache(key)

  File "...\Anaconda2\lib\site-packages\pandas\core\generic.py", line 1350, in _get_item_cache
    values = self._data.get(item)

  File "...\Anaconda2\lib\site-packages\pandas\core\internals.py", line 3290, in get
    loc = self.items.get_loc(item)

  File "...\Anaconda2\lib\site-packages\pandas\indexes\base.py", line 1947, in get_loc
    return self._engine.get_loc(self._maybe_cast_indexer(key))

  File "pandas\index.pyx", line 137, in pandas.index.IndexEngine.get_loc (pandas\index.c:4154)

  File "pandas\index.pyx", line 159, in pandas.index.IndexEngine.get_loc (pandas\index.c:4018)

  File "pandas\hashtable.pyx", line 675, in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:12368)

  File "pandas\hashtable.pyx", line 683, in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:12322)

KeyError: 'D'
AssertionError: Grouper and axis must be same length
我不确定发生了什么,但它似乎没有识别出
datetimeIndex
,即使当我执行
df7.info()
时,我返回:

<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 1542 entries, 2014-06-28 12:00:00 to 2014-08-31 12:00:00
Data columns (total 2 columns):
Observed    1542 non-null float64
WRF         1542 non-null float64
dtypes: float64(2)
memory usage: 36.1 KB

日期时间索引:1542条条目,2014-06-28 12:00:00至2014-08-31 12:00:00
数据列(共2列):
观察到1542个非空浮点64
WRF 1542非空浮点64
数据类型:float64(2)
内存使用率:36.1 KB
所以它似乎是
datetimeIndex
格式

非常感谢您的帮助,如果需要进一步澄清,我非常乐意提供更多信息