张儁义 2021-07-18 09:22 采纳率: 0%
浏览 21

请问我的数据可视化程序哪里出错了,结果和我预想的不一样

一个数据可视化的程序


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()


代码运行结果

img

时间是错误的,温度数值也没有显示

我写的不用函数的版本

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()



运行结果是正确的

img

请问, 第一个程序错在哪了 , 我找了半天没找多错误。

  • 写回答

1条回答 默认 最新

  • 天元浪子 Python领域优质创作者 2021-07-18 15:40
    关注

    读取CSV数据文件的代码,两个脚本大相径庭:前者,append操作在循环外;后者,append操作在循环体内。这就是问题所在。

    评论

报告相同问题?

问题事件

  • 创建了问题 7月18日

悬赏问题

  • ¥15 数据库原理及应用上机练习题
  • ¥30 征集Python提取PDF文字属性的代码
  • ¥15 如何联系真正的开发者而非公司
  • ¥15 有偿求苍穹外卖环境配置
  • ¥15 代码在keil5里变成了这样怎么办啊,文件图像也变了,
  • ¥20 Ue4.26打包win64bit报错,如何解决?(语言-c++)
  • ¥15 clousx6整点报时指令怎么写
  • ¥30 远程帮我安装软件及库文件
  • ¥15 关于#自动化#的问题:如何通过电脑控制多相机同步拍照或摄影(相机或者摄影模组数量大于60),并将所有采集的照片或视频以一定编码规则存放至规定电脑文件夹内
  • ¥15 Arduino的wifi连接,如何关闭低功耗模式?