Justetc Social Services (non-profit)Jan 31 · 5 min read
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inlinefrom __future__ import print_function
from ipywidgets import interact, interactive, fixed, interact_manual
import ipywidgets as widgetsnon_med_determinants = pd.read_excel('../data/non-med-determinants.xls')
non_med_determinants.head()
# find all indicators
non_med_determinants.set_index(['Indicator'])
indicators = pd.Index(non_med_determinants['Indicator']).unique()
# find all years
years = non_med_determinants['Data year'].dropna().unique()
indicators[0], years('Alcohol Consumption: Adults',
array(['2014', '2015', '2013', 2015, 'Not applicable', '2016'],
dtype=object))# sort years
years = [ int(aYear) for aYear in years if (aYear != 'Not applicable') and len( str(aYear).split(' ')) <= 1 ]
years = sorted(years)
years
all_years = [0] + years
all_years[0, 2013, 2014, 2015, 2015, 2016]plt.rcParams['figure.figsize'] = [10, 10]
def plot_non_med_determinants(year, indicator, bubble_scale, all_years=all_years):
# print('year, indicator', year, indicator)
plt.ion()
f = plt.figure()
ax = f.gca()
f.show()
#indicator_data = health_status.loc[ (health_status['Indicator'] == indicators[0] ) ]
if ( year > 1 ):
indicator_data = non_med_determinants.loc[ (non_med_determinants['Indicator'] == indicator) & (non_med_determinants['Data year'] == year) ]
ax.scatter(indicator_data['Data year'], indicator_data['Region'], s=indicator_data['Value'] * bubble_scale )
plt.show()
else:
for aYear in all_years:
indicator_data = non_med_determinants.loc[ (non_med_determinants['Indicator'] == indicator) & (non_med_determinants['Data year'] == aYear) ]
ax.scatter(indicator_data['Data year'], indicator_data['Region'], s = indicator_data['Value'] * bubble_scale )
f.canvas.draw()# create the interactive interface
def f(x):
return x
indicator = interactive(f, x=indicators);
display(indicator)
indicator.result
year = interactive(f, x=all_years);
display(year)
year.result
bubble_scale = interactive(f, x=(0, 100, 1));
display(bubble_scale)
bubble_scale.resultinteractive(children=(Dropdown(description='x', options=('Alcohol Consumption: Adults', 'Fruit Consumption: Ad…
interactive(children=(Dropdown(description='x', options=(0, 2013, 2014, 2015, 2015, 2016), value=0), Output())…
interactive(children=(IntSlider(value=50, description='x'), Output()), _dom_classes=('widget-interact',))
50plot_non_med_determinants(year.result, indicator.result, bubble_scale.result)C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\figure.py:445: UserWarning: Matplotlib is currently using module://ipykernel.pylab.backend_inline, which is a non-GUI backend, so cannot show the figure.
% get_backend())
plt.rcParams['figure.figsize'] = [20, 10]
def plot_non_med_determinants_by_regions(year, indicator, bubble_scale, all_years=all_years):
#print('year, indicator', year, indicator)
plt.ion()
f = plt.figure()
ax = f.gca()
f.show()
#indicator_data = health_status.loc[ (health_status['Indicator'] == indicators[0] ) ]
if ( year > 1 ):
#indicator_data = indicator_data [ indicator_data['Data year'] == year]
indicator_data = non_med_determinants.loc[ (non_med_determinants['Indicator'] == indicator) & (non_med_determinants['Data year'] == year) ]
#print(indicator_data)
ax.scatter(indicator_data['Region'], indicator_data['Value'], s=indicator_data['Value'] * bubble_scale )
plt.show()
else:
for aYear in all_years:
indicator_data = non_med_determinants.loc[ (non_med_determinants['Indicator'] == indicator) & (non_med_determinants['Data year'] == aYear) ]
ax.scatter(indicator_data['Region'], indicator_data['Value'], s = indicator_data['Value'] * bubble_scale )
f.canvas.draw()print('Select parameter to visualize across countries over years')
# create the interactive interface
def f(x):
return x
indicator = interactive(f, x=indicators);
display(indicator)
indicator.result
year = interactive(f, x=all_years);
display(year)
year.result
bubble_scale = interactive(f, x=(0, 100, 1));
display(bubble_scale)
bubble_scale.resultSelect parameter to visualize across countries over years
interactive(children=(Dropdown(description='x', options=('Alcohol Consumption: Adults', 'Fruit Consumption: Ad…
interactive(children=(Dropdown(description='x', options=(0, 2013, 2014, 2015, 2015, 2016), value=0), Output())…
interactive(children=(IntSlider(value=50, description='x'), Output()), _dom_classes=('widget-interact',))
50plot_non_med_determinants_by_regions(year.result, indicator.result, bubble_scale.result)
plt.rcParams['figure.figsize'] = [20, 10]
def plot_non_med_determinants_by_countries(year, indicator, bubble_scale, selected_countries, all_years=all_years):
#print('year, indicator', year, indicator)
plt.ion()
f = plt.figure()
ax = f.gca()
f.show()
#indicator_data = health_status.loc[ (health_status['Indicator'] == indicators[0] ) ]
if ( year > 1 ):
#indicator_data = indicator_data [ indicator_data['Data year'] == year]
indicator_data = non_med_determinants.loc[ (non_med_determinants['Indicator'] == indicator) & (non_med_determinants['Data year'] == str(year) ) & (non_med_determinants['Type of region'] == 'Country' ) ]
#print(indicator_data)
if ( len(selected_countries) > 1 ):
indicator_data = indicator_data[ indicator_data['Region'].isin(selected_countries)]
ax.scatter(indicator_data['Region'], indicator_data['Value'], s=indicator_data['Value'] * bubble_scale )
plt.show()
else:
for aYear in all_years:
indicator_data = non_med_determinants.loc[ (non_med_determinants['Indicator'] == indicator) & (non_med_determinants['Data year'] == str(aYear) ) & (non_med_determinants['Type of region'] == 'Country' ) ]
if ( len(selected_countries) > 1 ):
indicator_data = indicator_data[ indicator_data['Region'].isin(selected_countries)]
#print(indicator_data)
ax.scatter(indicator_data['Region'], indicator_data['Value'], s = indicator_data['Value'] * bubble_scale )
f.canvas.draw()countries = non_med_determinants[ non_med_determinants['Type of region'] == 'Country' ]['Region']
countries = sorted (countries.unique())
#countries = ['Canada'] + countries
if 'Canada' in countries:
countries = ['Canada'] + countries
#countriesprint('Select parameters to visualize across countries over years\n')
# create the interactive interface
def f(x):
return x
print('Select chart type:')
chart_types = ['Bubble', 'Bar', 'Pie', 'Line']
chart = interactive(f, x=chart_types);
display(chart)
chart.result
print('Select Indicator')
indicator_country = interactive(f, x=indicators);
display(indicator_country)
indicator_country.result
print('Select Year: 0 = all years')
year_country = interactive(f, x=all_years);
display(year_country)
year_country.result
print('Select bubble size')
bubble_scale_country = interactive(f, x=(0, 100, 1));
display(bubble_scale_country)
bubble_scale_country.result
print('Max country count to compare with')
country_count = interactive(f, x=range(1, 20));
display(country_count)
country_count.result
print('Select countries')
print('Compare with Canada')
all_countries = interactive(f, x=True);
display(all_countries)
all_countries.result
country_0 = interactive(f, x = countries);
country_1 = interactive(f, x = countries);
#country_str = "var%d = interactive(f, x = countries)"
display(country_0)
display(country_1)
country_0.result
print('Compare with Canada')
compare_canada = interactive(f, x=True);
display(compare_canada)
compare_canada.resultSelect parameters to visualize across countries over years
Select chart type:
interactive(children=(Dropdown(description='x', options=('Bubble', 'Bar', 'Pie', 'Line'), value='Bubble'), Out…
Select Indicator
interactive(children=(Dropdown(description='x', options=('Alcohol Consumption: Adults', 'Fruit Consumption: Ad…
Select Year: 0 = all years
interactive(children=(Dropdown(description='x', options=(0, 2013, 2014, 2015, 2015, 2016), value=0), Output())…
Select bubble size
interactive(children=(IntSlider(value=50, description='x'), Output()), _dom_classes=('widget-interact',))
Max country count to compare with
interactive(children=(Dropdown(description='x', options=(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16…
Select countries
Compare with Canada
interactive(children=(Checkbox(value=True, description='x'), Output()), _dom_classes=('widget-interact',))
interactive(children=(Dropdown(description='x', options=('Austria', 'Belgium', 'Chile', 'Czech Republic', 'Den…
interactive(children=(Dropdown(description='x', options=('Austria', 'Belgium', 'Chile', 'Czech Republic', 'Den…
Compare with Canada
interactive(children=(Checkbox(value=True, description='x'), Output()), _dom_classes=('widget-interact',))
Truecountry_0.result'Austria'selected_countries = []
if (all_countries.result == False):
selected_countries = [country_0.result, country_1.result]
#print(year_country.result, indicator_country.result, bubble_scale_country.result)
plot_non_med_determinants_by_countries(year_country.result, indicator_country.result, bubble_scale_country.result, selected_countries)
#print(country_0, country_1)
non_med_determinants.loc[ (non_med_determinants['Indicator'] == indicators[0]) & (non_med_determinants['Data year'] == '2013') & (non_med_determinants['Type of region'] == 'Country') ]
Appendix
"""
print('Select countries')
for i in range(country_count.result):
#country_str = interactive(f, x = countries);
country_str = "var%d = interactive(f, x = countries)"
display(country)
country.result
"""
for i in range(country_count.result):
country_str = "var%d = interactive(f, x = countries)" %(i)
exec(country_str)
country_str
for i in [0, 2]:
'var' + str(i)