python中的日期数据为';对象';可以';使用matplotlib沿x轴绘制t图
我使用的是matplotlib,有一个数据框python中的日期数据为';对象';可以';使用matplotlib沿x轴绘制t图,python,date,datetime,matplotlib,plot,Python,Date,Datetime,Matplotlib,Plot,我使用的是matplotlib,有一个数据框df,有两列Date(因变量)和Value(自变量)。我想沿x轴绘制Date,但当前数据集是对象,即使在转换为数值后也是如此。绘制这些日期的最佳方法是什么?我可以: 将日期转换为年月日或 将对象转换为matplotlib可以打印的其他数据类型 这两种选择都可以 我查看了datetime包的文档,但我真的不明白到底发生了什么。这是我的密码: import matplotlib.pyplot as plt from datetime import da
df
,有两列Date
(因变量)和Value
(自变量)。我想沿x轴绘制Date
,但当前数据集是对象,即使在转换为数值后也是如此。绘制这些日期的最佳方法是什么?我可以:
- 将日期转换为年月日或
- 将对象转换为matplotlib可以打印的其他数据类型
datetime
包的文档,但我真的不明白到底发生了什么。这是我的密码:
import matplotlib.pyplot as plt
from datetime import datetime, date,time
fig = plt.figure()
ax = fig.add_subplot(111)
x_points = df['Date']
x_points = x_points.convert_objects(convert_numeric=True)
>>> x_points
189 11/14/15
191 11/14/15
192 11/14/15
193 11/14/15
194 11/15/15
...
2910 2/20/16
2912 2/20/16
2914 2/20/16
2915 2/20/16
2917 2/20/16
这就是我用来策划的,但在我的策划中什么都没有出现
y_points = df['Value']
p = ax.plot(x_points, y_points, 'b')
ax.set_xlabel('x-points')
ax.set_ylabel('y-points')
ax.set_title('Simple XY point plot')
fig.show()
希望得到一个解决方案和一个解释来帮助我学习 不要使用普通Python
datetime
对象。。。它们永远是物体。尝试x_points=pd.to_datetime(x_points)
这将为您提供datetime64
dtypeSeries
。是的!成功了。我想我现在应该删除这个问题。谢谢!请查看我的其他问题: