沿x轴绘制日期时间

时间:2016-04-08 14:03:19

标签: csv datetime numpy matplotlib

您好我正在尝试在x轴上绘制日期时间数组。然而,我似乎有一个问题,实际上绘制它们并继续得到值的错误:

ValueError: invalid literal for long() with base 10: '2013-02-18 00:00:00'

有没有人有任何想法我错了?

数据本身的格式为四列,打印时如下所示:('04 / 03/2013',7.0,12.0,17.0)('11 / 03/2013',23.0,15.0,23.0)。 / p>

import csv
import numpy as np
import pylab as pl
import datetime
from datetime import datetime
data = np.genfromtxt('H:/AppliedGIS/Python/AssignmentData/GrowthDistribution/full.csv', names=True, usecols=(0, 1, 2, 3), delimiter= ',', skiprows=1, dtype =[('Date', 'S10'),('HIGH', '<f8'), ('Medium', '<f8'), ('Low', '<f8')])
print data
x =  [foo[0] for foo in data]
y =  [foo[1] for foo in data]
y2 = [foo[2] for foo in data]
y3 = [foo[3] for foo in data]
print x, y, y2, y3
dates = []
for i in x:
    date = datetime.datetime.strptime['i', '%d/%m/%Y']
    dates.append(date)

pl.plot(data[:, dates], data[:, y], '-r', label= 'High Stocking Rate')
pl.plot(data[:, dates], data[:, y2], '-g', label= 'Medium Stocking Rate')
pl.plot(data[:, dates], data[:, y3], '-b', label= 'Low Stocking Rate')
pl.title('Amount of Livestock Grazing per hectare', fontsize=18)
pl.ylabel('Livestock per ha')
pl.xlabel('Date')
pl.grid(True)
pl.ylim(0,100)
pl.show()

1 个答案:

答案 0 :(得分:0)

我立即在代码中看到两个问题。

date = datetime.datetime.strptime['i', '%d/%m/%Y'] should be
date = datetime.datetime.strptime('i', '%d/%m/%Y')

pl.plot(data[:, dates], data[:, y], '-r', label= 'High Stocking Rate')

Try this instead:
pl.plot_date(dates, y, '-r', label= 'High Stocking Rate')