一个数据可视化的程序
import csv
from datetime import datetime
from matplotlib import pyplot as plt
def get_weather_data(filename, dates, highs, lows, date_index,
high_index, low_index):
with open(filename) as f:
reader = csv.reader(f)
header_row = next(reader)
for row in reader:
current_date = datetime.strptime(row[date_index], '%Y/%m/%d')
try:
high = int(row[high_index])
low = int(row[low_index])
except ValueError:
print(f"Missing data for {current_date}")
else:
dates.append(current_date)
highs.append(high)
lows.append(low)
#获取锡特卡的气温数据
filename = 'data/sitka_weather_2018_simple.csv'
dates, highs, lows = [], [], []
get_weather_data(filename, dates, highs, lows, date_index=2,
high_index=5,low_index=6)
fig, ax = plt.subplots()
ax.plot(dates, highs, c='red', alpha=0.6)
ax.plot(dates, lows, c='blue', alpha=0.6)
plt.fill_between(dates, highs, lows, facecolor='blue', alpha=0.15)
#获取死亡谷的数据
filename = 'data/death_valley_2018_simple.csv'
dates, highs, lows = [], [], [] #重置列表
get_weather_data(filename, dates, highs, lows, date_index=2, high_index=4,
low_index=5) # 将死亡谷的数据添加到当前图表中。
ax.plot(dates, highs, c='red', alpha=0.3)
ax.plot(dates, lows, c='blue', alpha=0.3)
plt.fill_between(dates, highs, lows, facecolor='blue', alpha=0.05)
title = "Daily high and low temperatures - 2018"
title += "\nSitka, AK and Death Valley, CA"
plt.title(title, fontsize=24)
plt.xlabel('', fontsize=16)
fig.autofmt_xdate()
plt.ylabel("Temperature (F)", fontsize=16)
plt.tick_params(axis='both', which='major', labelsize=16)
plt.ylim(10, 130)
plt.show()
代码运行结果
时间是错误的,温度数值也没有显示
我写的不用函数的版本
import csv
import matplotlib.pyplot as plt
from datetime import datetime
filename_s = 'data/sitka_weather_2018_simple.csv'
filename_d = 'data/death_valley_2018_simple.csv'
with open(filename_s) as f:
reader = csv.reader(f)
header_row = next(reader)
dates, highs_s, lows_s = [], [], []
for row in reader:
crrent_date = datetime.strptime(row[2], '%Y/%m/%d')
try:
high = int(row[5])
low = int(row[6])
except ValueError:
print(f"Missing data for {crrent_date}")
else:
dates.append(crrent_date)
highs_s.append(high)
lows_s.append(low)
with open(filename_d) as f:
reader = csv.reader(f)
header_row = next(reader)
highs_d, lows_d = [], []
for row in reader:
#crrent_date = datetime.strptime(row[2], '%Y-%m-%d')
try:
high = int(row[4])
low = int(row[5])
except ValueError:
print(f"Missing data for {crrent_date}")
else:
highs_d.append(high)
lows_d.append(low)
#根据最高温度和最低温度绘制图形
#plt.style.use('seaborn')
fig, ax = plt.subplots(figsize=(15,9)) #设置图片尺寸
ax.plot(dates, highs_s, c='red', alpha=0.5) #alpha指定颜色的透明度
ax.plot(dates, lows_s, c='red', alpha=0.5)
ax.plot(dates, highs_d, c='blue', alpha=0.5) #alpha指定颜色的透明度
ax.plot(dates, lows_d, c='blue', alpha=0.5)
ax.fill_between(dates, highs_s, lows_s, facecolor='blue', alpha=0.1)
ax.fill_between(dates, highs_d, lows_d, facecolor='blue', alpha=0.3)
#fill_between填充颜色facecolor指定颜色
#设置图形的格式
title = "2018年最高温度\n 美国加利福尼亚州和死亡谷"
ax.set_title(title, fontsize=24)
ax.set_xlabel('', fontsize=16)
fig.autofmt_xdate()#改变x轴坐标的显示方法可以斜着表示,不用平着挤一堆
ax.set_ylabel('温度(F)', fontsize=16)
ax.tick_params(axis='both', which='major', labelsize=16)
plt.show()
运行结果是正确的
请问, 第一个程序错在哪了 , 我找了半天没找多错误。