3.读取CSV文件并进行可视化处理

import csv
import matplotlib.pyplot as plt
from datetime import datetime
plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签
plt.rcParams['axes.unicode_minus']=False #用来正常显示负号
filename = 'sitka_weather_07-2018_simple.csv'
highs = []
dates = []
with open(filename) as f:
reader = csv.reader(f)
header_row = next(reader)
# print(header_row)
for row in reader:
current_time = datetime.strptime(row[2],'%Y-%m-%d')
high = int(row[5])
highs.append(high)
dates.append(current_time)
print(highs)
# for index,column_header in enumerate(header_row):
# print(index,column_header)
fig ,ax =plt.subplots()
ax.plot(dates,highs,c='red')
fig.autofmt_xdate() #用来获取倾斜的标签,防止标签遮挡
ax.set_title("2018年七月每日最高温度",fontsize=24)
ax.set_xlabel('',fontsize='16')
ax.set_ylabel('温度(F)',fontsize='16')
plt.show()

可视化效果:
image

import csv
import matplotlib.pyplot as plt
from datetime import datetime
plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签
plt.rcParams['axes.unicode_minus']=False #用来正常显示负号
filename = 'Data/sitka_weather_2018_simple.csv'
highs = []
dates = []
lows = []
with open(filename) as f:
reader = csv.reader(f)
header_row = next(reader)
# print(header_row)
for row in reader:
current_time = datetime.strptime(row[2],'%Y-%m-%d')
high = int(row[5])
low = int(row[6])
highs.append(high)
dates.append(current_time)
lows.append(low)
print(highs)
# for index,column_header in enumerate(header_row):
# print(index,column_header)
fig ,ax =plt.subplots()
ax.plot(dates,highs,c='red',alpha=0.5)
ax.plot(dates,lows,c='red',alpha=0.5) #alpha参数设置颜色透明度
ax.fill_between(dates,highs,lows,facecolor='blue',alpha=0.8) #指定出填充区域
fig.autofmt_xdate() #用来获取倾斜的标签,防止标签遮挡
ax.set_title("2018年七月每日最高温度",fontsize=24)
ax.set_xlabel('',fontsize='16')
ax.set_ylabel('温度(F)',fontsize='16')
plt.show()

可视化:


__EOF__

本文作者DeepBrainBoy
本文链接https://www.cnblogs.com/tccjx/p/16908787.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   TCcjx  阅读(260)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示

喜欢请打赏

扫描二维码打赏

支付宝打赏