text_html_output_1.png
from IPython.display import display,clear_output

from ipywidgets import interact, interactive, fixed, interact_manual,VBox,HBox,Layout

import ipywidgets as widgets

import pandas as pd

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

import plotly.graph_objects as go

data = pd.read_csv('/kaggle/input/kaggle-survey-2020/kaggle_survey_2020_responses.csv', header=1, na_values='NaN')
data = data.fillna('Not Given')

def plot_map(grp_data):
    fig = go.Figure(data=go.Choropleth(locations=grp_data['Country'], z=grp_data['people'], text=grp_data['Country'], colorscale='earth', locationmode='country names', autocolorscale=False, reversescale=False, marker_line_color='darkgray', marker_line_width=0.5, colorbar_tickprefix='', colorbar_title='people'))
    fig.update_layout(title_text='People by country', height=500, width=500, geo=dict(showframe=False, showcoastlines=False, projection_type='equirectangular'), annotations=[dict(x=0.55, y=0.1, xref='paper', yref='paper', showarrow=False)])

def map_gender_plot(df):
    gender = data['What is your gender? - Selected Choice'].unique().tolist()
    xvar = widgets.Dropdown(options=gender, description='Gender:')
    def f(xvar_):
        grp_data = data[data['What is your gender? - Selected Choice'] == xvar_].groupby(by='In which country do you currently reside?').size().sort_values(ascending=False).reset_index()
        grp_data.columns = ['Country', 'people']
    a = interactive(f, xvar_=xvar)
    ctrl_map = HBox(a.children[:-1], layout=Layout(flex_flow='row wrap'))
    out_map = a.children[-1]
    return [ctrl_map, out_map]

def map_age_plot(df):
    age = data['What is your age (# years)?'].sort_values().unique().tolist()
    xvar = widgets.Dropdown(options=age, description='Age:')
    def f(xvar_):
        grp_data = data[data['What is your age (# years)?'] == xvar_].groupby(by='In which country do you currently reside?').size().sort_values(ascending=False).reset_index()
        grp_data.columns = ['Country', 'people']
    a = interactive(f, xvar_=xvar)
    ctrl_map = HBox(a.children[:-1], layout=Layout(flex_flow='row wrap'))
    out_map = a.children[-1]
    return [ctrl_map, out_map]

def map_education_plot(df):
    age = data['What is the highest level of formal education that you have attained or plan to attain within the next 2 years?'].sort_values().unique().tolist()
    xvar = widgets.Dropdown(options=age, description='Education:')
    def f(xvar_):
        grp_data = data[data['What is the highest level of formal education that you have attained or plan to attain within the next 2 years?'] == xvar_].groupby(by='In which country do you currently reside?').size().sort_values(ascending=False).reset_index()
        grp_data.columns = ['Country', 'people']
    a = interactive(f, xvar_=xvar)
    ctrl_map = HBox(a.children[:-1], layout=Layout(flex_flow='row wrap'))
    out_map = a.children[-1]
    return [ctrl_map, out_map]

def map_role_plot(df):
    age = data['Select the title most similar to your current role (or most recent title if retired): - Selected Choice'].sort_values().unique().tolist()
    xvar = widgets.Dropdown(options=age, description='Current Role:')
    def f(xvar_):
        grp_data = data[data['Select the title most similar to your current role (or most recent title if retired): - Selected Choice'] == xvar_].groupby(by='In which country do you currently reside?').size().sort_values(ascending=False).reset_index()
        grp_data.columns = ['Country', 'people']
    a = interactive(f, xvar_=xvar)
    ctrl_map = HBox(a.children[:-1], layout=Layout(flex_flow='row wrap'))
    out_map = a.children[-1]
    return [ctrl_map, out_map]

tab1 = HBox(map_gender_plot(data))
tab2 = HBox(map_age_plot(data))
tab3 = HBox(map_education_plot(data))
tab4 = HBox(map_role_plot(data))
tab = widgets.Tab(children=[tab1, tab2, tab3, tab4])
tab.set_title(0, 'Gender')
tab.set_title(1, 'Age')
tab.set_title(2, 'Education')
tab.set_title(3, 'Role')
display(tab)