text_html_output_2.png
from plotly.offline import init_notebook_mode, iplot

from plotly.offline import init_notebook_mode, iplot

import matplotlib.pyplot as plt

import pandas as pd

import plotly.graph_objs as go

import plotly.graph_objs as go

data = pd.read_csv('/kaggle/input/kaggle-survey-2020/kaggle_survey_2020_responses.csv')
df = data.iloc[1:, :]

init_notebook_mode(connected=True)
col = 'Q1'
v1 = df[col].value_counts().reset_index()
v1 = v1.rename(columns={col: 'count', 'index': col})
v1 = v1.sort_values(col)
trace1 = go.Bar(x=v1[col], y=v1['count'], marker=dict())
layout = {'title': 'Age Distribution', 'xaxis': {'title': 'Age Group'}, 'yaxis': {'title': 'Count'}}
fig = go.Figure(data=[trace1], layout=layout)

import plotly
import plotly.graph_objs as go
from plotly.offline import init_notebook_mode, iplot
init_notebook_mode(connected=True)
col = 'Q2'
grouped = df[col].value_counts().reset_index()
grouped = grouped.rename(columns={col: 'count', 'index': col})
trace = go.Pie(labels=grouped[col], values=grouped['count'], pull=[0.05, 0])
layout = {'title': 'Gender Distribution'}
fig = go.Figure(data=[trace], layout=layout)

d1 = df[df['Q2'] == 'Man']
d2 = df[df['Q2'] == 'Woman']
col = 'Q1'
v1 = d1[col].value_counts().reset_index()
v1 = v1.rename(columns={col: 'count', 'index': col})
v1['percent'] = v1['count'].apply(lambda x: 100 * x / sum(v1['count']))
v1 = v1.sort_values(col)
v2 = d2[col].value_counts().reset_index()
v2 = v2.rename(columns={col: 'count', 'index': col})
v2['percent'] = v2['count'].apply(lambda x: 100 * x / sum(v2['count']))
v2 = v2.sort_values(col)
trace1 = go.Bar(x=v1[col], y=v1['count'], name='Man', marker=dict(color='rgb(26, 118, 255)'))
trace2 = go.Bar(x=v2[col], y=v2['count'], name='Woman', marker=dict(color='rgb(55, 83, 109)'))
y = [trace1, trace2]
layout = {'title': 'Age Distribution over the Gender', 'bargap': 0.2, 'bargroupgap': 0.1, 'xaxis': {'title': 'Age Distribution'}, 'yaxis': {'title': 'Count'}}
fig = go.Figure(data=y, layout=layout)
fig.layout.template = 'presentation'

plt.style.use('fivethirtyeight')
col='Q3'
v2=df[col].value_counts().reset_index()
v2=v2.rename(columns={col:'count','index':col})
#v2['percent']=v2['count'].apply(lambda x : 100*x/sum(v2['count']))
v2=v2.sort_values(by='count',ascending=False)
plt.figure(figsize=(30,12))
barplot = plt.bar(v2.Q3,v2['count'],color='black')
for bar in barplot:
    yval = bar.get_height()
    plt.text(bar.get_x() + bar.get_width()/2.0, yval, int(yval), va='bottom') #va: vertical alignment y positional argument
    
plt.title(" Number of Respondents per Country")
plt.xticks(rotation=90)
plt.show()

key1 = "Master's degree"
key2 = "Bachelor's degree"
df1 = df[df['Q4'] == 'Master’s degree']
df2 = df[df['Q4'] == 'Bachelor’s degree']
nations = ['United States of America', 'Canada', 'Brazil', 'Mexico', 'Germany', 'Spain', 'France', 'Italy', 'India', 'Japan', 'China', 'South Korea']
nation_map = {'United States of America': 'USA', 'United Kingdom of Great Britain and Northern Ireland': 'UK'}
plt.figure(figsize=(15, 15))
vals = []
for j in range(len(nations)):
    country = nations[j]
    country_df = df[df['Q3'] == country]
    ddf1 = country_df[country_df['Q4'] == 'Master’s degree']
    ddf2 = country_df[country_df['Q4'] == 'Bachelor’s degree']
    plt.subplot(4, 4, j + 1)
    if j < 4:
        colors = ['orange', 'yellow']
    elif j < 8:
        colors = ['red', '#ff8ce0']
    else:
        colors = ['green', '#827ec4']
    vals.append(len(ddf1) / (len(ddf1) + len(ddf2)))
    plt.pie([len(ddf1), len(ddf2)], labels=['Mastor Degree', "Bachelor's Degree"], autopct='%1.0f%%', colors=colors, wedgeprops={'linewidth': 5, 'edgecolor': 'white'})
    if country in nation_map:
        country = nation_map[country]
    plt.title('$\\bf{' + country + '}$')