数据可视化笔记

处理csv文件:
import csv #csv模块包含在python标准库中
a.打开被处理文件 (sitka_weather_07-2014.csv)
用一次reader类中next()处理文件的第一行也就是每一列的列名字
------------------------------------------------
import csv

filename='sitka_weather_07-2014.csv'
with open(filename) as f:
reader=csv.reader(f)
header_row=next(reader)
print(header_row)


------------------------------------------------
可以用以下方法打印出文件头和其所在的位置:

------------------------------------------------
import csv

filename='sitka_weather_07-2014.csv'
with open(filename) as f:
reader=csv.reader(f)
header_row=next(reader)


for index,column_header in enumerate(header_row):
print(index,column_header)

------------------------------------------------
b.提取并读取数据:
读取的是第二列的数据(最高温度)
------------------------------------------------
import csv

filename='sitka_weather_07-2014.csv'
with open(filename) as f:
reader=csv.reader(f)
header_row=next(reader)
highs=[]
for row in reader:
high=int(row[1]) #第二行的数据(最高温度)并转化成更干净的字符串型
highs.append(high) #储存在一个列表中
print(highs)


------------------------------------------------
c.根据提取出来的数据进行绘制气温图表
------------------------------------------------
import csv
from matplotlib import pyplot as plt

filename='sitka_weather_07-2014.csv'
with open(filename) as f:
reader=csv.reader(f)
header_row=next(reader)
highs=[]
for row in reader:
high=int(row[1]) #第二行的数据(最高温度)并转化成更干净的字符串型
highs.append(high) #储存在一个列表中
print(highs)
#根据数据绘制图形
fig=plt.figure(dpi=128,figsize=(10,6))
plt.plot(highs,c='red')
#设置图形的格式
plt.title("Daily high temperatures, July 2014",fontsize=24)
plt.xlabel('',fontsize=16)
plt.ylabel("Temperature(F)",fontsize=16)
plt.tick_params(axis='both',which='major',labelsize=16)

plt.show()

------------------------------------------------
d.读取2014年一年的最高温度并绘图(添加时间)
------------------------------------------------
import csv
from datetime import datetime
from matplotlib import pyplot as plt

#filename='sitka_weather_07-2014.csv'
filename='sitka_weather_2014.csv'
with open(filename) as f:
reader=csv.reader(f)
header_row=next(reader)
dates,highs=[],[]
for row in reader:
current_data=datetime.strptime(row[0],"%Y-%m-%d")
dates.append(current_data)
high=int(row[1]) #第二行的数据(最高温度)并转化成更干净的字符串型
highs.append(high) #储存在一个列表中

#根据数据绘制图形
fig=plt.figure(dpi=128,figsize=(10,6))
plt.plot(dates,highs,c='red')
#设置图形的格式
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
xtitle='2014年最高气温'
plt.title(xtitle,fontsize=24)
plt.xlabel('',fontsize=16)
fig.autofmt_xdate()
ytitle='温度'
plt.ylabel(ytitle,fontsize=16)
plt.tick_params(axis='both',which='major',labelsize=16)

plt.show()
------------------------------------------------
e.想图表中加入最低气温(从而得到全年的气温差直观图)
------------------------------------------------
import csv
from datetime import datetime
from matplotlib import pyplot as plt

#filename='sitka_weather_07-2014.csv'
filename='sitka_weather_2014.csv'
with open(filename) as f:
reader=csv.reader(f)
header_row=next(reader)
dates,highs=[],[]
dates,highs,lows=[],[],[] #存放对最低气温
for row in reader:
current_data=datetime.strptime(row[0],"%Y-%m-%d")
dates.append(current_data)
high=int(row[1]) #第二行的数据(最高温度)并转化成更干净的int型
highs.append(high) #储存在一个列表中
low=int(row[3]) #获取第四列数据(最低温度)并转化成干净的int型
lows.append(low) #存放在列表中

#根据数据绘制图形
fig=plt.figure(dpi=128,figsize=(10,6))
plt.plot(dates,highs,c='red')
plt.plot(dates,lows,c='blue')
#设置图形的格式
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
xtitle='2014年全年气温差'
plt.title(xtitle,fontsize=24)
plt.xlabel('',fontsize=16)
fig.autofmt_xdate()
ytitle='温度'
plt.ylabel(ytitle,fontsize=16)
plt.tick_params(axis='both',which='major',labelsize=16)

plt.show()
--------------------------------------------------
f.为气温差区域着色
--------------------------------------------------
import csv
from datetime import datetime
from matplotlib import pyplot as plt

#filename='sitka_weather_07-2014.csv'
filename='sitka_weather_2014.csv'
with open(filename) as f:
reader=csv.reader(f)
header_row=next(reader)
dates,highs=[],[]
dates,highs,lows=[],[],[] #存放对最低气温
for row in reader:
current_data=datetime.strptime(row[0],"%Y-%m-%d")
dates.append(current_data)
high=int(row[1]) #第二行的数据(最高温度)并转化成更干净的int型
highs.append(high) #储存在一个列表中
low=int(row[3]) #获取第四列数据(最低温度)并转化成干净的int型
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='blue',alpha=0.1)
#设置图形的格式
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
xtitle='2014年全年气温差'
plt.title(xtitle,fontsize=24)
plt.xlabel('',fontsize=16)
fig.autofmt_xdate()
ytitle='温度'
plt.ylabel(ytitle,fontsize=16)
plt.tick_params(axis='both',which='major',labelsize=16)

plt.show()
----------------------------------------------------
JSON格式文件处理------未完待续

原文地址:https://www.cnblogs.com/yongqi-wang/p/www.html