python.matplotlib/datatime/CSV学习范例-读取CSV格式文件生成折线图
2017-10-01 16:01
1451 查看
import csv
from datetime import datetime
from matplotlib import pyplot as plt
filename='D:\python program\weather.csv'
#分析csv头文件
with open(filename) as f:
reader=csv.reader(f) #生成阅读器
header_row=next(reader) #将阅读器对象传给next返回下一行,本部读取头行
#下一行
#读取数据,获取日期(row[0])、最高气温row[1]、最低气温row[3]
dates,highs,lows=[],[],[]
for row in reader:
#对某个日期没有温度的异常处理
try:
#将日期字符串转换为表示日期的对象
current_date=datetime.strptime(row[0],"%Y-%m-%d")
high=int(row[1]) #字符串转换成数字让matplotlib读取
low=int(row[3])
except ValueError:
print(current_date,'missing data')
else:
dates.append(current_date)
highs.append(high)
lows.append(low)
#绘制图形
fig=plt.figure(dpi=128,figsize=(10,6))
plt.plot(dates,highs,c='red',alpha=0.5)
plt.plot(dates,lows,c='blue',alpha=0.5)
#给图标区域填充颜色
plt.fill_between(dates,highs,lows,facecolor='yellow',alpha=0.1)
#设置图形格式
plt.title('daily high and low temperatures, july 2014',fontsize=24)
plt.xlabel('',fontsize=16)
#绘制倾斜的日期标签
fig.autofmt_xdate()
plt.ylabel('temperature ',fontsize=16)
plt.tick_params(axis='both',which='major',labelsize=16)
plt.show()
from datetime import datetime
from matplotlib import pyplot as plt
filename='D:\python program\weather.csv'
#分析csv头文件
with open(filename) as f:
reader=csv.reader(f) #生成阅读器
header_row=next(reader) #将阅读器对象传给next返回下一行,本部读取头行
#下一行
#读取数据,获取日期(row[0])、最高气温row[1]、最低气温row[3]
dates,highs,lows=[],[],[]
for row in reader:
#对某个日期没有温度的异常处理
try:
#将日期字符串转换为表示日期的对象
current_date=datetime.strptime(row[0],"%Y-%m-%d")
high=int(row[1]) #字符串转换成数字让matplotlib读取
low=int(row[3])
except ValueError:
print(current_date,'missing data')
else:
dates.append(current_date)
highs.append(high)
lows.append(low)
#绘制图形
fig=plt.figure(dpi=128,figsize=(10,6))
plt.plot(dates,highs,c='red',alpha=0.5)
plt.plot(dates,lows,c='blue',alpha=0.5)
#给图标区域填充颜色
plt.fill_between(dates,highs,lows,facecolor='yellow',alpha=0.1)
#设置图形格式
plt.title('daily high and low temperatures, july 2014',fontsize=24)
plt.xlabel('',fontsize=16)
#绘制倾斜的日期标签
fig.autofmt_xdate()
plt.ylabel('temperature ',fontsize=16)
plt.tick_params(axis='both',which='major',labelsize=16)
plt.show()
相关文章推荐
- python matplotlib从文件中读取数据绘制折线图
- python.json/pygal.maps.world学习范例-读取json格式文件、生成世界地图
- python : pandas 读取csv 文件, matplotlib 画基金净值线
- python.matplotlib/random.choice学习范例-随机生成点图
- 使用pandas模块读取csv文件和excel表格,并用matplotlib画图的方法
- python matplotlib从文件中读取数据绘制散点图
- python 数据可视化 matplotlib学习二 : 对折线图使用颜色映射
- python数据分析——matplotlib生成折线图,散点图和直方图
- Python学习笔记(4)--Matplotlib同时画柱状图与折线图
- 利用pandas模块读取csv文件和excel表格,并用matplotlib画图
- python 数据可视化 matplotlib学习三:生成随机漫步数据并使用matplotlib呈现
- Python matplotlib生成图片背景透明
- Python matplotlib生成图片背景透明
- python学习笔记(matplotlib下载安装)
- Python matplotlib生成图片背景透明
- python matplotlib阶段性总结——word转txt、绘图、文件操作
- Python matplotlib生成图片背景透明
- Python matplotlib生成图片背景透明
- Python matplotlib生成图片背景透明
- Python学习-centos7.0下matplotlib安装及问题解决