from IPython.core.display import HTML
def css_styling():
sheet = '../css/custom.css'
styles = open(sheet, "r").read()
return HTML(styles)
css_styling()
import matplotlib.pyplot as plt
import numpy as np
import pickle
%matplotlib inline
The purpose of the exersise is to manipulate and plot the current weather of a number of European cities. The data has been downloaded from Openweather, and has been loaded for you below using the given function read_weather
.
The function returns a list of dictionaries, each of which contains the name of the cities along with a large number of the weather parameters. Before you start the exercise, print various parts of Data
familiarise yourself with the content.
def read_weather():
with open('data/weather.pkl', 'rb') as f:
return pickle.load(f)
# The file weather.pkl contains a list of dictionaries
Data = read_weather()
Data[0]
We want to manipulate the data so it is easier to plot. Create a new dictionary with the country names as keys (name
entries in Data
) and the values being the tuple of (lattitude, temperature)
corresponding to that country.
Plot the temperature of the cities (y) vs lattitude (x)
ax.annotate
# Implement Q1 part 1 here:
# -------------------------
# tempr_dict = ...
# Run this cell to tests if you have completed part 1 correctly:
assert(all(key in tempr_dict for key in ['Berlin', 'Kiev', 'London', 'Moscow', 'Southampton'])),\
'keys of your weather dictionary should be Berlin, Kiev, London, Moscow, Southampton'
assert(len(tempr_dict['Moscow']) == 2), "Entries in your dictionary should be a tuple of two values"
# Part 2:
Temperature is not the only parameter in the data. By extending the code you wrote above, plot the remaining variables
Instead of storing the value in your dictionary as (lattitude, temp)
, use the tuple (lattitude, main
) where main
is the dictionary containing temp
, pressure
and humidity
etc. entries from the original Data
list
Write a function which takes the weather dictionary and a string variable name (e.g. 'temp'
, 'pressure'
...) as arguments and plots the chosen variable against lattitude for all countries in the weather dictionary
# Implement Q2 pt 1 here
# -----------------------
# weather_dict = ...
# Implement Q2 pt 2 here
# -----------------------
def plot_weather_lattitude(weather_dictionary, var_name):
# YOUR CODE HERE
return None
# Here are the variables that should be in your data
weather_vars = ['temp', 'temp_max', 'temp_min', 'pressure', 'humidity']
# Loop over the variable strings above and plot them using your
# plot_weather_lattitude function:
for var_name in weather_vars:
plot_weather_lattitude(weather_dict, var_name)