【发布时间】:2019-12-30 08:36:59
【问题描述】:
我编写了一个 python 程序来将 csv 文件转换为图表。 它在图表中显示 1900,但我不需要它。 请帮我看看问题出在哪里。 谢谢。
import csv
from matplotlib import pyplot as plt
from datetime import datetime
filename='/home/pi/env_watcher/temp/env_report.csv'
with open(filename) as f:
reader=csv.reader(f)
header_row=next(reader)
dates,ttemps,ctemps,thumis,chumis=[],[],[],[],[]
for row in reader:
current_date=datetime.strptime(row[0],'%b %d %H:%M:%S')
dates.append(current_date)
ttemp=float(row[1])
ttemps.append(ttemp)
ctemp=float(row[2])
ctemps.append(ctemp)
thumi=float(row[3])
thumis.append(thumi)
chumi=float(row[4])
chumis.append(chumi)
fig=plt.figure(dpi=128,figsize=(10,6))
plt.plot(dates,thumis,c='red',alpha=0.5)
plt.plot(dates,chumis,c='blue',alpha=0.5)
plt.title('Weekly Humidity',fontsize=24)
plt.xlabel('',fontsize=16)
plt.ylabel('Humidity(%)',fontsize=16)
plt.tick_params(axis='both',which='major',labelsize=16)
fig.autofmt_xdate()
plt.savefig("/home/pi/env_watcher/temp/env_humi.png")
plt.show()
csv文件内容如下
......
8月25日 05:10:13,30,26.8,70,45.0
8月25日 05:20:13,30,26.8,70,44.8
8月25日 05:30:15,30,26.8,70,45.5
8月25日 05:40:13,30,26.8,70,45.5
8月25日 05:50:13,30,26.9,70,46.1
8月25日 06:00:13,30,26.9,70,46.3
8月25日 06:10:13,30,26.9,70,46.8
8月25日 06:20:13,30,26.9,70,46.8
......
【问题讨论】:
-
这是我的 csv 文件。请尝试一下,谢谢。 drive.google.com/open?id=1ZPYx4hRIIAPtlI3idYJKsgtGMASRnANa
标签: python python-3.x csv datetime matplotlib