首页 > 解决方案 > 无法在 x 轴 matplotlib 中绘制日期时间

问题描述

我正在尝试使用此代码在 x 轴上绘制日期时间

import matplotlib.dates as md
from datetime import datetime


figure = plt.figure(figsize=(15,10))
ax=plt.gca()
xfmt = md.DateFormatter('%Y-%m-%d %H:%M:%S.%f')
ax.xaxis.set_major_formatter(xfmt)
dates = df.timestamp.apply(lambda x: datetime.strptime(str(x), '%Y-%m-%d %H:%M:%S.%f')).values
datenums=md.date2num(dates)
_=plt.plot(x=datenums,y= values)

我的数据框包含这样的日期,2018-05-09 13:27:31.319283但我收到此错误:

ValueError:无法将 -1 转换为日期。如果将非日期时间值传递给需要日期时间对象的轴,这通常会发生。

标签: pythondatetimematplotlib

解决方案


推荐阅读