如何在Python中使用pyplot绘制日期与值的折线图?
我在绘制简单折线图时遇到问题,y轴上有数字值,其中y轴上有日期: 我的python代码如下所示:如何在Python中使用pyplot绘制日期与值的折线图?,python,datetime,matplotlib,plot,Python,Datetime,Matplotlib,Plot,我在绘制简单折线图时遇到问题,y轴上有数字值,其中y轴上有日期: 我的python代码如下所示: i = 0 #iterator for weather data xAxis = [] yAxis = [] for trainingDate in observations[:,0]: #getting values in x and y axis for the desired chart while weatherAttribute[0]
i = 0 #iterator for weather data
xAxis = []
yAxis = []
for trainingDate in observations[:,0]: #getting values in x and y axis for the desired chart
while weatherAttribute[0][i] != trainingDate:
xAxis.append(trainingDate)
yAxis.append(weatherAttribute[1][i])
i = i + 1
i = 0
fig=plt.figure()
graph = fig.add_subplot(111)
i = 0
for xdate in xAxis: #format of xdate is '2007-05-29'
graph.plot(DT(xdate).strftime("%Y-%b-%d"), yAxis[i])
i = i + 1
plt.show()
weatherAttribute列表在其中存储日期和值,其中as observations列表仅包含日期。在匹配两个列表的日期时,我想在图表上显示weatherAttribute列表的相应值。简而言之,所需的图表应该在x轴上有日期,在y轴上有数值
但我在这一行遇到了错误:
graph.plot(DT(xdate).strftime("%Y-%b-%d"), yAxis[i])
错误消息是:
TypeError: 'module' object is not callable
我的进口是:
import datetime as DT
from matplotlib import pyplot as plt
我相信这是一个简单的问题,但我已经搜索了allot,无法解决它。很抱歉,我是Python初学者。感谢您拨出时间等待建议 它在错误中:
DT(xdate)
是错误的,因为它试图调用DT
,就好像它是一个函数一样。您需要xdate.strftime(“%Y-%b-%d”)
。执行此操作后,我现在收到以下错误:AttributeError:“str”对象没有属性“strftime”,那么您没有将xdate
转换为datetime.date
对象DT.datetime.strtime(xdate,'%Y-%m-%d')
转换它。对于'2007-05-29'格式的日期(即YYYY-MM-DD),使用@cphlewis注释的datetime.strtime(xdate,'%Y-%m-%d')
。如果成功了,谢谢你们抽出时间:)