【问题标题】:How to remove years in csv chart如何删除csv图表中的年份
【发布时间】: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
......

输出:

【问题讨论】:

标签: python python-3.x csv datetime matplotlib


【解决方案1】:

看起来您不需要在此处进行转换。只需使用

for row in reader:
    current_date=row[0]

或者如果您只需要日期和月份 用途:

current_date=datetime.strptime("Aug 25 05:10:13",'%b %d %H:%M:%S').strftime("%m-%d")

【讨论】:

  • 我修改代码如下。 .... header_row=next(reader) dates,ttemps,ctemps,thumis,chumis=[],[],[],[],[] for row in reader: # current_date=datetime.strptime(row[0] ,'%b %d %H:%M:%S') current_date=row[0] dates.append(current_date) ttemp=float(row1) .... 并创建一个这样的图表。 drive.google.com/open?id=1J14uf-GcwfTUE48s79dsXIqTK-mlDMsQX轴有点拥挤,有没有办法简化?
【解决方案2】:

使用 xaxis 上的 set_major_formatter() 根据需要设置日期格式:

import csv
from matplotlib import pyplot as plt
from datetime import datetime
import matplotlib.dates as mdates

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.axes[0].xaxis.set_major_formatter(mdates.DateFormatter('%m-%d'))  # Won't show year
fig.autofmt_xdate()
plt.savefig("/home/pi/env_watcher/temp/env_humi.png")
plt.show()

【讨论】:

  • 问题解决了。非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-06
  • 1970-01-01
  • 2021-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多