!
"#$%&'()*+$'(%,)+-
!"#$%&'"#()$&(*+,-$.(/&0#-(/$%&#"(12#,("34,4$.56()&$7(8$(9,0&'-(*&"##:
!"#$%&'"#()$&(*+"(#"'$,-("-.*.$,(/&"(+"&"0(12-(3$4"(*$(5,$6(6+/*(7$%(*+.,5(/8$%*(97*+$,
:&/#+(:$%&#";(<3"/#"('$,#.-"&(*/5.,=(/(8&.")(#%&4"70(1)(7$%2-(3.5"(*$(5,$6(6+",(/--.*.$,/3
&"#$%&'"#(/&"(/4/.3/83">(7$%('/,(#.=,(%<()$&("?/.3(,$*.@'/*.$,#(+"&"0
!"#$%&"'()*)+,-.%/0)12
!"#$%&'()*+#,-+).&/+00-1&2345+6(738
!"#9%&:+(8;+00
!"#<%&=>5036-
!"#"%&?6377&,34-7)(@&A63BC@)
!"#D%&E-7)(8F&).-& country_codes &G3BC0-
H+@*&)3&730C)(387I
!"#$%&'()*+#,-+).&/+00-1&2345+6(738
E.-&)-45-6+)C6-&7@+0-7&38&).-&'()*+&+8B&,-+).&/+00-1&F6+5.7&6-J-@)&).-&B(K-6-8)&6+8F-7&3;&).-
B+)+I&E3&+@@C6+)-01&@345+6-&).-&)-45-6+)C6-&6+8F-&(8&'()*+&)3&).+)&3;&,-+).&/+00-1L&13C&8--B
(B-8)(@+0&7@+0-7&38&).-&1#+>(7I&2.+8F-&).-&7-))(8F7&;36&).-&1#+>(7&38&38-&36&M3).&3;&).-&@.+6)7&(8
N(FC6-7&!"#O&+8B&!"#"L&+8B&4+*-&+&B(6-@)&@345+6(738&M-)P--8&)-45-6+)C6-&6+8F-7&(8&'()*+&+8B
,-+).&/+00-1&Q36&+81&)P3&50+@-7&13C&P+8)&)3&@345+6-RI&S3C&@+8&+073&)61&503))(8F&).-&)P3&B+)+&7-)7
38&).-&7+4-&@.+6)I
E.-& pyplot &;C8@)(38& ylim() &+003P7&13C&)3&7-)&).-&0(4()7&3;&TC7)&).-&1#+>(7I&U;&13C&-V-6&8--B&)3&75-@(;1
).-&0(4()7&3;&).-&>#+>(7L&).-6-W7&+&@366-7538B(8F& xlim() &;C8@)(38&+7&P-00I
import csv
from datetime import datetime
from matplotlib import pyplot as plt
# Get dates, high, and low temperatures from file.
filename = 'sitka_weather_2014.csv'
with open(filename) as f:
reader = csv.reader(f)
header_row = next(reader)
dates, highs, lows = [], [], []
for row in reader:
try:
current_date = datetime.strptime(row[0], "%Y-%m-%d")
high = int(row[1])
low = int(row[3])
except ValueError:
print(current_date, 'missing data')
else:
dates.append(current_date)
highs.append(high)
lows.append(low)
# Plot data.
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)
# Format plot.
title = "Daily high and low temperatures - 2014\nSitka, AK"
plt.title(title, fontsize=20)
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, 120)
plt.show()
XC)5C)%
Y7(8F&).-&7+4-&0(4()7&;36&).-& ylim() &;C8@)(38&P().&).-&,-+).&/+00-1&B+)+&6-7C0)7&(8&+&@.+6)&).+)&.+7
).-&7+4-&7@+0-%
E.-6-&+6-&+&8C4M-6&3;&P+17&13C&@+8&+5563+@.&503))(8F&M3).&B+)+&7-)7&38&).-&7+4-&@.+6)I&U8&).-
;3003P(8F&730C)(38L&P-&5C)&).-&@3B-&;36&6-+B(8F&).-&@7V&Z0-&(8)3&+&;C8@)(38I&[-&).-8&@+00&()&38@-&)3
F6+M&).-&.(F.7&+8B&03P7&;36&'()*+&M-;36-&4+*(8F&).-&@.+6)L&+8B&).-8&@+00&).-&;C8@)(38&+&7-@38B&)(4-
)3&+BB&,-+).&/+00-1W7&B+)+&)3&).-&->(7)(8F&503)I&E.-&@30367&.+V-&M--8&+BTC7)-B&70(F.)01&)3&4+*-&-+@.
03@+)(38W7&B+)+&B(7)(8@)I
import csv
from datetime import datetime
from matplotlib import pyplot as plt
def get_weather_data(filename, dates, highs, lows):
"""Get the highs and lows from a data file."""
with open(filename) as f:
reader = csv.reader(f)
header_row = next(reader)
# dates, highs, lows = [], [], []
for row in reader:
try:
current_date = datetime.strptime(row[0], "%Y-%m-%d")
high = int(row[1])
low = int(row[3])
except ValueError:
print(current_date, 'missing data')
else:
dates.append(current_date)
highs.append(high)
lows.append(low)
# Get weather data for Sitka.
dates, highs, lows = [], [], []
get_weather_data('sitka_weather_2014.csv', dates, highs, lows)
# Plot Sitka weather data.
fig = plt.figure(dpi=128, figsize=(10, 6))
plt.plot(dates, highs, c='red', alpha=0.6)
plt.plot(dates, lows, c='blue', alpha=0.6)
plt.fill_between(dates, highs, lows, facecolor='blue', alpha=0.15)
# Get Death Valley data.
dates, highs, lows = [], [], []
get_weather_data('death_valley_2014.csv', dates, highs, lows)
# Add Death Valley data to current plot.
plt.plot(dates, highs, c='red', alpha=0.3)
plt.plot(dates, lows, c='blue', alpha=0.3)
plt.fill_between(dates, highs, lows, facecolor='blue', alpha=0.05)
# Format plot.
title = "Daily high and low temperatures - 2014"
title += "\nSitka, AK and Death Valley, CA"
plt.title(title, fontsize=20)
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, 120)
plt.show()
)35
!"#9%&:+(8;+00
2.337-&+81&03@+)(38&13CW6-&(8)-6-7)-B&(8L&+8B&4+*-&+&V(7C+0(\+)(38&).+)&503)7&()7&6+(8;+00I&')+6)&M1
;3@C7(8F&38&38-&438).W7&B+)+L&+8B&).-8&38@-&13C6&@3B-&(7&P36*(8FL&6C8&()&;36&+&;C00&1-+6W7&B+)+I
!"#$%!"#$!%&'!(')!*+,!)&*&!(-,!.#/!*+01!,2&34-,!+,/,5
import csv
from datetime import datetime
from matplotlib import pyplot as plt
# Get dates and rainfall data from data file.
# Rainfall data is in column 19.
filename = 'sitka_rainfall_2015.csv'
with open(filename) as f:
reader = csv.reader(f)
header_row = next(reader)
dates, rainfalls = [], []
for row in reader:
try:
current_date = datetime.strptime(row[0], "%Y-%m-%d")
rainfall = float(row[19])
except ValueError:
print(current_date, 'missing data')
else:
dates.append(current_date)
rainfalls.append(rainfall)
# Plot data.
fig = plt.figure(dpi=128, figsize=(10, 6))
plt.plot(dates, rainfalls, c='blue', alpha=0.5)
plt.fill_between(dates, rainfalls, facecolor='blue', alpha=0.2)
# Format plot.
title = "Daily rainfall amounts - 2015\nSitka, AK"
plt.title(title, fontsize=20)
plt.xlabel('', fontsize=16)
fig.autofmt_xdate()
plt.ylabel("Rainfall (in)", fontsize=16)
plt.tick_params(axis='both', which='major', labelsize=16)
plt.show()
XC)5C)%
)35
!"#<%&=>5036-
?-8-6+)-&+&;-P&436-&V(7C+0(\+)(387&).+)&->+4(8-&+81&3).-6&P-+).-6&+75-@)&13CW6-&(8)-6-7)-B&(8&;36
+81&03@+)(387&13CW6-&@C6(3C7&+M3C)I
U&0(V-&(8&+&6+(8;36-7)L&73&U&P+7&(8)-6-7)-B&(8&50+1(8F&P().&).-&6+(8;+00&B+)+I&U&@+0@C0+)-B&).-&@C4C0+)(V-
6+(8;+00&;36&).-&1-+6L&+8B&503))-B&).+)&3V-6&).-&B+(01&6+(8;+00I&=V-8&+;)-6&0(V(8F&(8&).(7&6+(8L&UW4
7C656(7-B&)3&7--&.3P&4C@.&P-&F-)I
import csv
from datetime import datetime
from matplotlib import pyplot as plt
# Get dates and rainfall data from data file.
# Rainfall data is in column 19.
filename = 'sitka_rainfall_2015.csv'
with open(filename) as f:
reader = csv.reader(f)
header_row = next(reader)
dates, rainfalls, totals = [], [], []
for row in reader:
try:
current_date = datetime.strptime(row[0], "%Y-%m-%d")
rainfall = float(row[19])
except ValueError:
print(current_date, 'missing data')
else:
dates.append(current_date)
rainfalls.append(rainfall)
if totals:
totals.append(totals[-1] + rainfall)
else:
totals.append(rainfall)
# Plot data.
fig = plt.figure(dpi=128, figsize=(10, 6))
plt.plot(dates, rainfalls, c='blue', alpha=0.5)
plt.fill_between(dates, rainfalls, facecolor='blue', alpha=0.2)
plt.plot(dates, totals, c='blue', alpha=0.75)
plt.fill_between(dates, totals, facecolor='blue', alpha=0.05)
# Format plot.
title = "Daily rainfall amounts and cumulative rainfall - 2015\nSitka, AK"
plt.title(title, fontsize=20)
plt.xlabel('', fontsize=16)
fig.autofmt_xdate()
plt.ylabel("Rainfall (in)", fontsize=16)
plt.tick_params(axis='both', which='major', labelsize=16)
plt.show()
XC)5C)%
)35
!"#"%&?6377&,34-7)(@&A63BC@)
E.-&X5-8&]83P0-BF-&N3C8B+)(38&4+(8)+(87&+&B+)+&7-)&@38)+(8(8F&).-&F6377&B34-7)(@&563BC@)
Q?,AR&;36&-+@.&@3C8)61&(8&).-&P360BL&P.(@.&13C&@+8&Z8B&+)&.))5%^^B+)+I3*;8I36F^B+)+^@36-^FB5^I
,3P803+B&).-&_'X`&V-67(38&3;&).(7&B+)+&7-)L&+8B&503)&).-&?,A&3;&-+@.&@3C8)61&(8&).-&P360B&;36&).-
437)&6-@-8)&1-+6&(8&).-&B+)+&7-)I
!"#$%!6.!7#$8/,!+&90':!*/#$;-,!)#<'-#&)0':!*+,!=>?@!(-,!.#/!ABC!)&*&D!7#$!%&'!*/7!*+01!)0/,%*!-0'E5!6.
*+&*!1*0--!)#,1'8*!<#/ED!689,!1*#/,)!&!%#47!#.!*+,!(-,!+,/,5
!"#$%&&H-&+P+6-&).+)&734-&V-67(387&3;&).(7&B+)+&Z0-&.+V-&1-+67&(8&aC3)-7L&+8B&734-&.+V-&).-&1-+67
C8aC3)-BI&[.-8&).-&1-+67&+6-&aC3)-B&).-1W6-&)6-+)-B&+7&7)6(8F7I&[.-8&).-1W6-&C8aC3)-B&).-1W6-
)6-+)-B&+7&8C4-6(@+0&B+)+I&S3C&4+1&8--B&)3&@.+8F-&@345+6(7387&0(*-& if gdp_dict['Year'] ==
'2014' &)3& if gdp_dict['Year'] == 2014: I
!"#$%!F+,!',<,1*!9,/10#'!#.!C7:&-!+&')-,1!<#/-)!3&41!0'!&!1-0:+*-7!)0G,/,'*!<&7!*+&'!<+&*!<&1
),1%/0;,)!0'!*+,!;##E5!6.!7#$!+&9,'8*!1,,'!0*!&-/,&)7D!*&E,!&!-##E!&*!*+,!$4)&*,1!.#/!H+&4*,/!IJ5
import json
import pygal
from pygal.style import LightColorizedStyle as LCS, RotateStyle as RS
from pygal.maps.world import World
from country_codes import get_country_code
# Load the data into a list.
filename = 'global_gdp.json'
with open(filename) as f:
gdp_data = json.load(f)
# Build a dictionary of gdp data.
cc_gdps = {}
for gdp_dict in gdp_data:
if gdp_dict['Year'] == '2014':
country_name = gdp_dict['Country Name']
gdp = int(float(gdp_dict['Value']))
code = get_country_code(country_name)
if code:
cc_gdps[code] = gdp
# Group the countries into 3 gdp levels.
# Less than 5 billion, less than 50 billion, >= 50 billion.
# Also, convert to billions for displaying values.
cc_gdps_1, cc_gdps_2, cc_gdps_3 = {}, {}, {}
for cc, gdp in cc_gdps.items():
if gdp < 5000000000:
cc_gdps_1[cc] = round(gdp / 1000000000)
elif gdp < 50000000000:
cc_gdps_2[cc] = round(gdp / 1000000000)
else:
cc_gdps_3[cc] = round(gdp / 1000000000)
# See how many countries are in each level.
print(len(cc_gdps_1), len(cc_gdps_2), len(cc_gdps_3))
wm_style = RS('#336699', base_style=LCS)
wm = World(style=wm_style)
wm.title = 'Global GDP in 2014, by Country (in billions USD)'
wm.add('0-5bn', cc_gdps_1)
wm.add('5bn-50bn', cc_gdps_2)
wm.add('>50bn', cc_gdps_3)
wm.render_to_file('global_gdp.svg')
XC)5C)%
)35
!"#D%&E-7)(8F&).-& country_codes &G3BC0-
[.-8&P-&P63)-&).-& country_codes &43BC0-L&P-&C7-B& print &7)+)-4-8)7&)3&@.-@*&P.-).-6&).-
get_country_code() &;C8@)(38&P36*-BI&[6()-&+&5635-6&)-7)&;36&).(7&;C8@)(38&C7(8F&P.+)&13C&0-+68-B&(8
2.+5)-6&!!I
import unittest
from country_codes import get_country_code
class CountryCodesTestCase(unittest.TestCase):
"""Tests for country_codes.py."""
def test_get_country_code(self):
country_code = get_country_code('Andorra')
self.assertEqual(country_code, 'ad')
country_code = get_country_code('United Arab Emirates')
self.assertEqual(country_code, 'ae')
country_code = get_country_code('Afghanistan')
self.assertEqual(country_code, 'af')
unittest.main()
XC)5C)%
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
)35
!"#$%&'()*+$'(%,)+-'.+'/*.&#*.&-0'1"'-$/*##$-+2
!"#$%&'()%*'$%()+),'-).%/0%1#-23/%4'()$%3$#+(%-")%5'06'+%-")6)%/0%7'$8+%98+(: