python - 无法在 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 转换为日期。如果将非日期时间值传递给需要日期时间对象的轴,这通常会发生。
解决方案
推荐阅读
- knex.js - 我可以链接 Knex Raw 命令吗?
- c - 从 C 调用本地 Julia 包
- maven - 从 Sonatype Repo Manager 下载 Spring-Boot-Starter-Project
- r - 为什么 RShiny 执行代码与 R 不同
- javascript - JavaScript遍历两个数组并获得单个数组
- kotlin - 结果为空。无法使用 Ksoap2-android 从 SOAP api 获得响应(使用 Kotlin)
- python - 不带关键字参数的 Python 库函数
- python - 使用 python 部署网络安全组的问题:NetworkSecurityGroup' 对象没有属性 'lower'
- android - 如何在使用垂直 ViewPager 时仅滑动一半 Fragment
- ibm-odm - 如何使用列表创建对象/实体 XOM-BOM