import numpy as np
import pandas as pd
import altair as alt
import seaborn as sns
import weather
plants
import importlib
importlib.reload(weather)
importlib.reload(plants)
# showing only 10 rows when trying to print the whole pandas dataset
pd.options.display.max_rows = 15
import json # need it for json.dumps
import altair as alt
from altair.vega import v3
from IPython.display import HTML
# Create the correct URLs for require.js to find the Javascript libraries
vega_url = 'https://cdn.jsdelivr.net/npm/vega@' + v3.SCHEMA_VERSION
vega_lib_url = 'https://cdn.jsdelivr.net/npm/vega-lib'
vega_lite_url = 'https://cdn.jsdelivr.net/npm/vega-lite@' + alt.SCHEMA_VERSION
vega_embed_url = 'https://cdn.jsdelivr.net/npm/vega-embed@3'
noext = "?noext"
paths = {
'vega': vega_url + noext,
'vega-lib': vega_lib_url + noext,
'vega-lite': vega_lite_url + noext,
'vega-embed': vega_embed_url + noext
}
workaround = """
requirejs.config({{
baseUrl: 'https://cdn.jsdelivr.net/npm/',
paths: {paths}
}});
"""
HTML("".join((
"<script>",
workaround.format(paths=json.dumps(paths)),
"</script>",
"This code block sets up embedded rendering in HTML."
)))
# Define the function for rendering
def add_autoincrement(render_func):
# Keep track of unique <div/> IDs
cache = {}
def wrapped(chart, id="vega-chart", autoincrement=True):
"""Render an altair chart directly via javascript.
This is a workaround for functioning export to HTML.
(It probably messes up other ways to export.) It will
cache and autoincrement the ID suffixed with a
number (e.g. vega-chart-1) so you don't have to deal
with that.
"""
if autoincrement:
if id in cache:
counter = 1 + cache[id]
cache[id] = counter
else:
cache[id] = 0
actual_id = id if cache[id] == 0 else id + '-' + str(cache[id])
else:
if id not in cache:
cache[id] = 0
actual_id = id
return render_func(chart, id=actual_id)
# Cache will stay defined and keep track of the unique div Ids
return wrapped
@add_autoincrement
def render(chart, id="vega-chart"):
# This below is the javascript to make the chart directly using vegaEmbed
chart_str = """
<div id="{id}"></div><script>
require(["vega-embed"], function(vegaEmbed) {{
const spec = {chart};
vegaEmbed("#{id}", spec, {{defaultStyle: true}}).catch(console.warn);
}});
</script>
"""
return HTML(
chart_str.format(
id=id,
chart=json.dumps(chart) if isinstance(chart, dict) else chart.to_json(indent=None)
)
)
First, I am geting the data from the weather api for the appropriate months
# shorthand notation for the relelvant cities
AA = "Ann Arbor, MI"
MSK = "Minsk, Belarus"
# this are the date blocks to fetch
april = ['04-15','04-30']
may = ['05-01','05-31']
june = ['06-01','06-30']
july = ['07-01','07-31']
august = ['08-01','08-31']
september = ['09-01','09-30']
def get_weather_data(city,months,years,file):
# Request to API and Dumping data to CSV
data = weather.get_weather_data(city,years,months)
weather.dump_to_csv(file, days = data)
# reading weather into pandas dataframe
df = pd.read_csv(file)
# coverting the date column into a 'datetime' column
df.date = df.date.astype('datetime64[ns]')
# making the column with average temperature
df['avgtempC'] = (df['maxtempC'] + df['mintempC'])/2
# returning the result
return df
#### GETTING WEATHER FOR ANN ARBOR ####
city = AA
months = [may,june,july,august]
years = [2012,2013,2014,2015,2016,2017,2018]
file = 'aa_weather.csv'
# getting weather data for each day of given month
aa_weather_df = get_weather_data(city,months,years,file)
# need to calculate averages for each year
aa_weather_mean_by_year_df = aa_weather_df.groupby(aa_weather_df.date.dt.year).mean()
#### GETTING WEATHER FOR MINSK ####
city = MSK
months = [may,june,july,august]
years = [2012,2013,2014,2015,2016,2017,2018]
file = 'msk_weather.csv'
# getting weather data for each day of given month
msk_weather_df = get_weather_data(city,months,years,file)
# need to calculate averages for each year
msk_weather_mean_by_year_df = msk_weather_df.groupby(msk_weather_df.date.dt.year).mean()
brush = alt.selection(type='interval', encodings=['x'])
msk = alt.Chart(msk_weather_df).mark_line().encode(
x=alt.X('date:T', scale={'domain': brush.ref()}),
y=alt.Y('humidity:Q',scale = {'domain':(40, 100)})
).properties(
width=600,
height=150
).encode(color='city:N')
aa = alt.Chart(aa_weather_df).mark_line().encode(
x=alt.X('date:T', scale={'domain': brush.ref()}),
y=alt.Y('humidity:Q',scale = {'domain':(40, 100)})
).properties(
width=600,
height=150
).encode(color='city:N')
lower =(msk.properties(
height=60,
width=600
)+aa.add_selection(brush)).properties(title="Interractive mini-map. Select using cursor the areas you'd like to zoom into")
render(alt.vconcat(msk,aa,msk+aa,lower).properties(title='Humidity in Minsk, Bealrus and Ann Arbor, MI, USA').configure_mark(opacity=0.8))
msk = msk.encode(y=alt.Y('avgtempC:Q'))
aa = aa.encode(y=alt.Y('avgtempC:Q'))
plot = alt.vconcat(msk,aa,msk+aa,lower).properties(title='Temperature in Minsk and Ann Arbor')
render(plot)
msk = msk.encode(y=alt.Y('cloudcover:Q',scale = {'domain':(-10, 110)}))
aa = aa.encode(y=alt.Y('cloudcover:Q',scale = {'domain':(-10, 110)}))
render(alt.vconcat(msk,aa,msk+aa,lower).properties(title='Cloudcover in Minsk, Bealrus and Ann Arbor, MI, USA'))