简体   繁体   English

无法绘制日期时间 pandas 系列与 matplotlib

[英]Trouble plotting datetime pandas series with matplotlib

在此处输入图像描述

I'm trying to plot "created_at" versus "rating".我正在尝试 plot “created_at”与“评级”。

"Created_at" is datetime format “Created_at”是日期时间格式

在此处输入图像描述

Why is the x-axis being shown as float values instead of dates like I am intending with the line为什么 x 轴显示为浮点值,而不是像我打算使用该行的日期

plt.plot(data['created_at'].values, data['rating'],"b.", alpha =0.5)

shown in the bottom picture如下图所示

The easiest way to achieve this is to define a temporary dataframe with the datetimes as index (see this SO post ):实现此目的的最简单方法是定义一个临时 dataframe 以日期时间为索引(请参阅此 SO 帖子):

rating_vs_creationdate = data.copy()
rating_vs_creationdate = rating_vs_creationdate.set_index('created_at')
rating_vs_creationdate = rating_vs_creationdate['rating']
plt.plot(rating_vs_creationdate)

Maybe you will run into problems with the date format.也许你会遇到日期格式的问题。 The link provided by MrFuppes in the comments gives an easy date formatting example. MrFuppes 在评论中提供的链接提供了一个简单的日期格式示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM