Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python中使用pyplot绘制日期与值的折线图?_Python_Datetime_Matplotlib_Plot - Fatal编程技术网

如何在Python中使用pyplot绘制日期与值的折线图?

如何在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]

我在绘制简单折线图时遇到问题,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] != 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')
。如果成功了,谢谢你们抽出时间:)