Climatograph¶
A climatograph (or climograph) is a visualization of the monthly average precipitation and temperature at a particular location to show the climate there.
In this notebook we are using climate data from The World Bank because we are allowed to share and remix their content.
Select the next cell and click the ▶Run
button to import the code libraries that we will use.
!pip install geopy
import pandas as pd
import plotly.graph_objs as go
import numpy as np
from geopy.geocoders import Nominatim
geolocator = Nominatim(user_agent='Callysto Climatograph')
print('Libraries successfully imported.')
Requirement already satisfied: geopy in /home/mikel/anaconda3/lib/python3.7/site-packages (2.0.0)
Requirement already satisfied: geographiclib<2,>=1.49 in /home/mikel/anaconda3/lib/python3.7/site-packages (from geopy) (1.50)
Libraries successfully imported.
Data¶
In the following cell, change the city or town name in location = 'Calgary, AB'
to something like location = 'Honolulu'
.
You can also change year = 2016
to any year between 1991
and 2016
.
Run
the cell to get and show the data.
location = 'Calgary, AB'
coordinates = geolocator.geocode(location)
latitude = coordinates.latitude # if geolocator isn't working, you can manually set the lat and long here
longitude = coordinates.longitude
precipitation_url = 'https://climateknowledgeportal.worldbank.org/api/data/get-download-data/historical/pr/1901-2016/'+str(latitude)+'$cckp$'+str(longitude)+'/'+str(latitude)+'$cckp$'+str(longitude)
df_precipitation_all = pd.read_csv(precipitation_url)
df_precipitation = df_precipitation_all.drop(columns=[' Longitude', ' Latitude', ' Year']).replace(' Average','',regex=True).replace(' ','',regex=True).rename(columns={'Rainfall - (MM)':'Precipitation (mm)', ' Statistics':'Month'})
dfp = df_precipitation.groupby('Month').aggregate(np.mean)
temperature_url = 'https://climateknowledgeportal.worldbank.org/api/data/get-download-data/historical/tas/1991-2016/'+str(latitude)+'$cckp$'+str(longitude)+'/'+str(latitude)+'$cckp$'+str(longitude)
df_temperature_all = pd.read_csv(temperature_url)
df_temperature = df_temperature_all.drop(columns=[' Longitude', ' Latitude', ' Year']).replace(' Average','',regex=True).replace(' ','',regex=True).rename(columns={'Temperature - (Celsius)':'Temperature (°C)', ' Statistics':'Month'})
dft = df_temperature.groupby('Month').aggregate(np.mean)
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
climate_data = dfp.join(dft)
climate_data.index = pd.CategoricalIndex(climate_data.index, categories=months, ordered=True)
climate_data = climate_data.sort_index()
climate_data
Precipitation (mm) | Temperature (°C) | |
---|---|---|
Month | ||
Jan | 16.463793 | -8.776923 |
Feb | 13.843966 | -6.119231 |
Mar | 22.156035 | -2.688462 |
Apr | 35.161207 | 3.015385 |
May | 62.089656 | 8.023077 |
Jun | 96.690519 | 11.803846 |
Jul | 64.767242 | 15.165385 |
Aug | 58.593966 | 14.300000 |
Sep | 44.335346 | 10.061539 |
Oct | 21.901724 | 4.261539 |
Nov | 19.450000 | -2.792308 |
Dec | 15.482759 | -7.769231 |
Graph¶
Run
the next cell to make a graph.
trace1 = go.Bar(x=climate_data.index.values.tolist(),y=climate_data['Precipitation (mm)'],name='Precipitation')
trace2 = go.Scatter(x=climate_data.index.values.tolist(),y=climate_data['Temperature (°C)'],name='Average Temperature (°C)',yaxis='y2')
layout = go.Layout(
title= ('Climatograph for '+location+' ('+str(latitude)+','+str(longitude)+')'),
yaxis=dict(title='Total Precipitation (mm)', titlefont=dict(color='blue'), tickfont=dict(color='blue')),
yaxis2=dict(title='Average Temperature (°C)', titlefont=dict(color='red'), tickfont=dict(color='red'), overlaying='y', side='right'),
showlegend=False)
fig = go.Figure(data=[trace1, trace2], layout=layout)
fig.update_yaxes(showgrid=False)
fig.show()
On the graph you can mouseover to check values and also zoom in and out.
You can download your climatograph using the 📷 button.
Climateograph for a Single Year¶
Run the following code if you’d prefer a single-year climatograph.
location = 'Calgary, AB'
year = 2016
# year can be any between 1901 and 2016
coordinates = geolocator.geocode(location)
latitude = coordinates.latitude # if geolocator isn't working, you can manually set the lat and long here
longitude = coordinates.longitude
precipitation_url = 'https://climateknowledgeportal.worldbank.org/api/data/get-download-data/historical/pr/1901-2016/'+str(latitude)+'$cckp$'+str(longitude)+'/'+str(latitude)+'$cckp$'+str(longitude)
df_precipitation_all = pd.read_csv(precipitation_url)
df_precipitation = df_precipitation_all[df_precipitation_all[' Year']==year].drop(columns=[' Longitude', ' Latitude', ' Year']).replace(' Average','',regex=True).replace(' ','',regex=True)
dfp = df_precipitation.rename(columns={'Rainfall - (MM)':'Precipitation (mm)', ' Statistics':'Month'}).set_index('Month')
temperature_url = 'https://climateknowledgeportal.worldbank.org/api/data/get-download-data/historical/tas/1991-2016/'+str(latitude)+'$cckp$'+str(longitude)+'/'+str(latitude)+'$cckp$'+str(longitude)
df_temperature_all = pd.read_csv(temperature_url)
df_temperature = df_temperature_all[df_temperature_all[' Year']==year].drop(columns=[' Longitude', ' Latitude', ' Year']).replace(' Average','',regex=True).replace(' ','',regex=True)
dft = df_temperature.rename(columns={'Temperature - (Celsius)':'Temperature (°C)', ' Statistics':'Month'}).set_index('Month')
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
climate_data = dfp.join(dft)
climate_data.index = pd.CategoricalIndex(climate_data.index, categories=months, ordered=True)
climate_data = climate_data.sort_index()
trace1 = go.Bar(x=climate_data.index.values.tolist(),y=climate_data['Precipitation (mm)'],name='Precipitation')
trace2 = go.Scatter(x=climate_data.index.values.tolist(),y=climate_data['Temperature (°C)'],name='Average Temperature (°C)',yaxis='y2')
layout = go.Layout(
title= (str(year)+' Climatograph for '+location+' ('+str(latitude)+','+str(longitude)+')'),
yaxis=dict(title='Total Precipitation (mm)', titlefont=dict(color='blue'), tickfont=dict(color='blue')),
yaxis2=dict(title='Average Temperature (°C)', titlefont=dict(color='red'), tickfont=dict(color='red'), overlaying='y', side='right'),
showlegend=False)
fig = go.Figure(data=[trace1, trace2], layout=layout)
fig.update_yaxes(showgrid=False)
fig.show()
Animated Climatograph¶
If you are interested in animating graphs over time, the following two code cells might be a good place to start.
import plotly.express as px
max_rainfall = df_precipitation_all['Rainfall - (MM)'].max()
fig = px.bar(df_precipitation_all.replace(' Average','',regex=True), x=' Statistics', y='Rainfall - (MM)', animation_frame=' Year', range_y=[0,max_rainfall])
fig.show()
max_temperature = df_temperature_all['Temperature - (Celsius)'].max()
min_temperature = df_temperature_all['Temperature - (Celsius)'].min()
fig = px.line(df_temperature_all.replace(' Average','',regex=True), x=' Statistics', y='Temperature - (Celsius)', animation_frame=' Year', range_y=[min_temperature,max_temperature])
fig.show()