Python 如何删除csv图表中的年份
我编写了一个python程序,将csv文件转换为图表。 图表上显示1900,但我不需要它。 请帮我检查问题出在哪里。 谢谢 csv文件内容如下所示Python 如何删除csv图表中的年份,python,python-3.x,csv,datetime,matplotlib,Python,Python 3.x,Csv,Datetime,Matplotlib,我编写了一个python程序,将csv文件转换为图表。 图表上显示1900,但我不需要它。 请帮我检查问题出在哪里。 谢谢 csv文件内容如下所示 …… 八月25日05:10:13,30,26.8,70,45.0 八月25日05:20:13,30,26.8,70,44.8 八月25日05:30:15,30,26.8,70,45.5 八月25日05:40:13,30,26.8,70,45.5 八月25日05:50:13,30,26.9,70,46.1 8月25日06:00:13,30,26.9,7
……
八月25日05:10:13,30,26.8,70,45.0
八月25日05:20:13,30,26.8,70,44.8
八月25日05:30:15,30,26.8,70,45.5
八月25日05:40:13,30,26.8,70,45.5
八月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
输出:
看起来您不需要在此处进行转换。只用
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")
看起来您不需要在此处进行转换。只用
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")
在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()
在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()
这是我的csv文件。请试一试,谢谢。这是我的csv文件。请试一试,谢谢。我修改代码如下。。。。读卡器中的行的标题行=下一(读卡器)日期,ttemps,ctemps,thumis,chumis=[],[],[],[],[],[],[],[],[],[]当前日期=日期时间。strTime(行[0],“%b%d%H:%M:%S”)当前日期=行[0]日期。追加(当前日期)ttemp=浮动(行1)。。。。然后创建一个这样的图表。X轴有点拥挤,有没有办法简化它?我修改代码如下。。。。读卡器中的行的标题行=下一(读卡器)日期,ttemps,ctemps,thumis,chumis=[],[],[],[],[],[],[],[],[],[]当前日期=日期时间。strTime(行[0],“%b%d%H:%M:%S”)当前日期=行[0]日期。追加(当前日期)ttemp=浮动(行1)。。。。然后创建一个这样的图表。X轴有点拥挤,有没有办法简化它?问题解决了。非常感谢。问题已经解决了。多谢各位。