Python数据可视化(处理天气数据)

Python数据可视化(处理天气数据)

从csv文件中导入数据。

import csv

with open('sitka_weather_07-2014.csv', 'r') as f:
    reader = csv.reader(f)
    for row in reader:
        print(row)
['AKDT', 'Max TemperatureF', 'Mean TemperatureF', 'Min TemperatureF', 'Max Dew PointF', 'MeanDew PointF', 'Min DewpointF', 'Max Humidity', ' Mean Humidity', ' Min Humidity', ' Max Sea Level PressureIn', ' Mean Sea Level PressureIn', ' Min Sea Level PressureIn', ' Max VisibilityMiles', ' Mean VisibilityMiles', ' Min VisibilityMiles', ' Max Wind SpeedMPH', ' Mean Wind SpeedMPH', ' Max Gust SpeedMPH', 'PrecipitationIn', ' CloudCover', ' Events', ' WindDirDegrees']
['2014-7-1', '64', '56', '50', '53', '51', '48', '96', '83', '58', '30.19', '30.00', '29.79', '10', '10', '10', '7', '4', '', '0.00', '7', '', '337']
['2014-7-2', '71', '62', '55', '55', '52', '46', '96', '80', '51', '29.81', '29.75', '29.66', '10', '9', '2', '13', '5', '', '0.14', '7', 'Rain', '327']
['2014-7-3', '64', '58', '53', '55', '53', '51', '97', '85', '72', '29.88', '29.86', '29.81', '10', '10', '8', '15', '4', '', '0.01', '6', '', '258']
['2014-7-4', '59', '56', '52', '52', '51', '50', '96', '88', '75', '29.91', '29.89', '29.87', '10', '9', '2', '9', '2', '', '0.07', '7', 'Rain', '255']
['2014-7-5', '69', '59', '50', '52', '50', '46', '96', '72', '49', '29.88', '29.82', '29.79', '10', '10', '10', '13', '5', '', '0.00', '6', '', '110']
['2014-7-6', '62', '58', '55', '51', '50', '46', '80', '71', '58', '30.13', '30.07', '29.89', '10', '10', '10', '20', '10', '29', '0.00', '6', 'Rain', '213']
['2014-7-7', '61', '57', '55', '56', '53', '51', '96', '87', '75', '30.10', '30.07', '30.05', '10', '9', '4', '16', '4', '25', '0.14', '8', 'Rain', '211']
['2014-7-8', '55', '54', '53', '54', '53', '51', '100', '94', '86', '30.10', '30.06', '30.04', '10', '6', '2', '12', '5', '23', '0.84', '8', 'Rain', '159']
['2014-7-9', '57', '55', '53', '56', '54', '52', '100', '96', '83', '30.24', '30.18', '30.11', '10', '7', '2', '9', '5', '', '0.13', '8', 'Rain', '201']
['2014-7-10', '61', '56', '53', '53', '52', '51', '100', '90', '75', '30.23', '30.17', '30.03', '10', '8', '2', '8', '3', '', '0.03', '8', 'Rain', '215']
['2014-7-11', '57', '56', '54', '56', '54', '51', '100', '94', '84', '30.02', '30.00', '29.98', '10', '5', '2', '12', '5', '', '1.28', '8', 'Rain', '250']
['2014-7-12', '59', '56', '55', '58', '56', '55', '100', '97', '93', '30.18', '30.06', '29.99', '10', '6', '2', '15', '7', '26', '0.32', '8', 'Rain', '275']
['2014-7-13', '57', '56', '55', '58', '56', '55', '100', '98', '94', '30.25', '30.22', '30.18', '10', '5', '1', '8', '4', '', '0.29', '8', 'Rain', '291']
['2014-7-14', '61', '58', '55', '58', '56', '51', '100', '94', '83', '30.24', '30.23', '30.22', '10', '7', '0', '16', '4', '', '0.01', '8', 'Fog', '307']
['2014-7-15', '64', '58', '55', '53', '51', '48', '93', '78', '64', '30.27', '30.25', '30.24', '10', '10', '10', '17', '12', '', '0.00', '6', '', '318']
['2014-7-16', '61', '56', '52', '51', '49', '47', '89', '76', '64', '30.27', '30.23', '30.16', '10', '10', '10', '15', '6', '', '0.00', '6', '', '294']
['2014-7-17', '59', '55', '51', '52', '50', '48', '93', '84', '75', '30.16', '30.04', '29.82', '10', '10', '6', '9', '3', '', '0.11', '7', 'Rain', '232']
['2014-7-18', '63', '56', '51', '54', '52', '50', '100', '84', '67', '29.79', '29.69', '29.65', '10', '10', '7', '10', '5', '', '0.05', '6', 'Rain', '299']
['2014-7-19', '60', '57', '54', '55', '53', '51', '97', '88', '75', '29.91', '29.82', '29.68', '10', '9', '2', '9', '2', '', '0.00', '8', '', '292']
['2014-7-20', '57', '55', '52', '54', '52', '50', '94', '89', '77', '29.92', '29.87', '29.78', '10', '8', '2', '13', '4', '', '0.31', '8', 'Rain', '155']
['2014-7-21', '69', '60', '52', '53', '51', '50', '97', '77', '52', '29.99', '29.88', '29.78', '10', '10', '10', '13', '4', '', '0.00', '5', '', '297']
['2014-7-22', '63', '59', '55', '56', '54', '52', '90', '84', '77', '30.11', '30.04', '29.99', '10', '10', '10', '9', '3', '', '0.00', '6', 'Rain', '240']
['2014-7-23', '62', '58', '55', '54', '52', '50', '87', '80', '72', '30.10', '30.03', '29.96', '10', '10', '10', '8', '3', '', '0.00', '7', '', '230']
['2014-7-24', '59', '57', '54', '54', '52', '51', '94', '84', '78', '29.95', '29.91', '29.89', '10', '9', '3', '17', '4', '28', '0.06', '8', 'Rain', '207']
['2014-7-25', '57', '55', '53', '55', '53', '51', '100', '92', '81', '29.91', '29.87', '29.83', '10', '8', '2', '13', '3', '', '0.53', '8', 'Rain', '141']
['2014-7-26', '57', '55', '53', '57', '55', '54', '100', '96', '93', '29.96', '29.91', '29.87', '10', '8', '1', '15', '5', '24', '0.57', '8', 'Rain', '216']
['2014-7-27', '61', '58', '55', '55', '54', '53', '100', '92', '78', '30.10', '30.05', '29.97', '10', '9', '2', '13', '5', '', '0.30', '8', 'Rain', '213']
['2014-7-28', '59', '56', '53', '57', '54', '51', '97', '94', '90', '30.06', '30.00', '29.96', '10', '8', '2', '9', '3', '', '0.61', '8', 'Rain', '261']
['2014-7-29', '61', '56', '51', '54', '52', '49', '96', '89', '75', '30.13', '30.02', '29.95', '10', '9', '3', '14', '4', '', '0.25', '6', 'Rain', '153']
['2014-7-30', '61', '57', '54', '55', '53', '52', '97', '88', '78', '30.31', '30.23', '30.14', '10', '10', '8', '8', '4', '', '0.08', '7', 'Rain', '160']
['2014-7-31', '66', '58', '50', '55', '52', '49', '100', '86', '65', '30.31', '30.29', '30.26', '10', '9', '3', '10', '4', '', '0.00', '3', '', '217']

由于reader是一个生成器(next方法调用),且为了更好的观察文件,先把文件的头打出来,即第一行。

with open('sitka_weather_07-2014.csv', 'r') as f:
    reader = csv.reader(f)
    header_row = next(reader)
    
    for index, content in enumerate(header_row):
        print(index, content)
0 AKDT
1 Max TemperatureF
2 Mean TemperatureF
3 Min TemperatureF
4 Max Dew PointF
5 MeanDew PointF
6 Min DewpointF
7 Max Humidity
8  Mean Humidity
9  Min Humidity
10  Max Sea Level PressureIn
11  Mean Sea Level PressureIn
12  Min Sea Level PressureIn
13  Max VisibilityMiles
14  Mean VisibilityMiles
15  Min VisibilityMiles
16  Max Wind SpeedMPH
17  Mean Wind SpeedMPH
18  Max Gust SpeedMPH
19 PrecipitationIn
20  CloudCover
21  Events
22  WindDirDegrees

这里我们关注的是最高气温。即第一列和第二列的数据(也就是我们对所有行进行处理,取每一行的前两个数据)。

with open('sitka_weather_07-2014.csv', 'r') as f:
    reader = csv.reader(f)
    #先把文件头取出来
    header_row = next(reader)
    date = []
    max_temperature = []
    for row in reader:
        date.append(row[0])
        max_temperature.append(int(row[1]))
print(date, max_temperature)
    
['2014-7-1', '2014-7-2', '2014-7-3', '2014-7-4', '2014-7-5', '2014-7-6', '2014-7-7', '2014-7-8', '2014-7-9', '2014-7-10', '2014-7-11', '2014-7-12', '2014-7-13', '2014-7-14', '2014-7-15', '2014-7-16', '2014-7-17', '2014-7-18', '2014-7-19', '2014-7-20', '2014-7-21', '2014-7-22', '2014-7-23', '2014-7-24', '2014-7-25', '2014-7-26', '2014-7-27', '2014-7-28', '2014-7-29', '2014-7-30', '2014-7-31'] [64, 71, 64, 59, 69, 62, 61, 55, 57, 61, 57, 59, 57, 61, 64, 61, 59, 63, 60, 57, 69, 63, 62, 59, 57, 57, 61, 59, 61, 61, 66]

先画出图。

import matplotlib.pyplot as plt

plt.plot(max_temperature)
plt.show()

png

对图进行一些美化。

#设置图片大小
fig = plt.figure(dpi=128, figsize=(10, 6))
plt.plot(max_temperature, linewidth=2)

#设置标题以及x,y轴标签
plt.title('Daily high temperatures, July 2014', fontsize=24)
plt.xlabel('Day', fontsize=18)
plt.ylabel('Temperature(F)', fontsize=18)

plt.show()

png

由于背景是黑色的,可以通过将字体的格式设置为一个字典传入title和label方法中。

#设置图片大小
fig = plt.figure(dpi=128, figsize=(10, 6))
plt.plot(max_temperature, linewidth=2)
#设置字体
font1 = {'family': 'Times New Roman',
        'color':  'white',
        'weight': 'normal',
        'size': 24
        }
font2 = {'family': 'Times New Roman',
         'color': 'white',
         'weight': 'normal',
         'size': 18
        }
#设置标题以及x,y轴标签
plt.title('Daily high temperatures, July 2014', fontdict=font1)
plt.xlabel('Day', fontdict=font2)
plt.ylabel('Temperature(F)', fontdict=font2)
plt.tick_params()

plt.show()

png博客里背景变成白色了(T^T),总之可以通过字典的形式来设置字体的格式。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值