matplotlib - 使用datetimeindex绘制一个序列; TypeError:无法比较类型'时间戳'类型'浮动'

时间:2018-01-22 03:27:04

标签: python-3.5 datetimeindex

这是代码:

def showbug_TemBiased(dt):
    ax1 = plt.subplot(1,1,1)
    legendlist=[]
    for i in range(4):
        power='CH{}power(KW)'.format(i+1)
        tem=[col for col in dt.columns if col.find('CH{}'.format(i+1))!=-1 and col.find('CHWRT')!=-1]
        dt_analysis=dt.loc[:,power]
        powline,=ax1.plot(dt_analysis,label=power)
        legendlist.append(powline)
    x_ax_date=pd.date_range(dt_analysis.index[0],dt_analysis.index[-1],freq='5min')
    ax1.xaxis.set_major_formatter(mdate.DateFormatter('%Y-%m-%d %H:%M:%S'))
    #plt.xticks(x_ax_date)
    ax1.legend(handles=legendlist,loc='upper center',bbox_to_anchor=(0.45,0.9))
    plt.grid(True)
    #plt.show()
    print(x_ax_date)
showbug_TemBiased(dt)

这是错误堆栈:

"D:\Program Files\Python\Python35\python.exe" "C:/Users/HASEE/PycharmProjects/MingFeng_DataAnalysis/for_mingfeng/exhibition of MingFeng data.py"
Traceback (most recent call last):
  File "C:/Users/HASEE/PycharmProjects/MingFeng_DataAnalysis/for_mingfeng/exhibition of MingFeng data.py", line 37, in <module>
    showbug_TemBiased(dt)
  File "C:/Users/HASEE/PycharmProjects/MingFeng_DataAnalysis/for_mingfeng/exhibition of MingFeng data.py", line 34, in showbug_TemBiased
    plt.xticks(x_ax_date)
  File "D:\Program Files\Python\Python35\lib\site-packages\matplotlib\pyplot.py", line 1615, in xticks
    locs = ax.set_xticks(args[0])
  File "D:\Program Files\Python\Python35\lib\site-packages\matplotlib\axes\_base.py", line 3003, in set_xticks
    ret = self.xaxis.set_ticks(ticks, minor=minor)
  File "D:\Program Files\Python\Python35\lib\site-packages\matplotlib\axis.py", line 1647, in set_ticks
    self.set_view_interval(min(ticks), max(ticks))
  File "D:\Program Files\Python\Python35\lib\site-packages\matplotlib\axis.py", line 1996, in set_view_interval
    self.axes.viewLim.intervalx = (min(vmin, vmax, Vmin),
  File "pandas\_libs\tslib.pyx", line 1166, in pandas._libs.tslib._Timestamp.__richcmp__
  TypeError: Cannot compare type 'Timestamp' with type 'float'

打印x_ax_date可以看到:

DatetimeIndex(['2017-09-16 00:00:00', '2017-09-16 00:05:00',
               '2017-09-16 00:10:00', '2017-09-16 00:15:00',
               '2017-09-16 00:20:00', '2017-09-16 00:25:00',
               '2017-09-16 00:30:00', '2017-09-16 00:35:00',
               '2017-09-16 00:40:00', '2017-09-16 00:45:00',
               ...
               '2017-09-20 09:10:00', '2017-09-20 09:15:00',
               '2017-09-20 09:20:00', '2017-09-20 09:25:00',
               '2017-09-20 09:30:00', '2017-09-20 09:35:00',
               '2017-09-20 09:40:00', '2017-09-20 09:45:00',
               '2017-09-20 09:50:00', '2017-09-20 09:55:00'],
              dtype='datetime64[ns]', length=1272, freq='5T')

当我尝试x_ax_date.to_pydatetime()时,它可以绘制。但是,x_ax_date.to_pydatetime().value似乎比dt_analysis.index.value小得多。

为什么会出错?

1 个答案:

答案 0 :(得分:0)

最后,我使用数字索引(而不是datatimeindex)绘制数据集,并使用plt.xtick()设置x_axis标签。它按预期工作。