Q018 - A che livello di istituzione pubblica era stato organizzato il bando?


In [1]:
# -*- coding: UTF-8 -*-

# Render our plots inline
%matplotlib inline 

import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import seaborn
import shutil

pd.set_option('display.mpl_style', 'default') # Make the graphs a bit prettier, overridden by seaborn
pd.set_option('display.max_columns', None) # Display all the columns
plt.rcParams['font.family'] = 'sans-serif' # Sans Serif fonts for all the graphs

# Reference for color palettes: http://web.stanford.edu/~mwaskom/software/seaborn/tutorial/color_palettes.html

# Change the font
matplotlib.rcParams.update({'font.family': 'Source Sans Pro'})

In [2]:
# Load csv file first
data = pd.read_csv("data/lab-survey.csv", encoding="utf-8")

In [3]:
# Check data
#data[0:4] # Equals to data.head()

In [4]:
data['D16[SQ011]'].replace(u'Sì', 'Si', inplace=True) # Get rid of accented characters 

# Get rid of the people who did not answer to this section
data = data.loc[data['D16[SQ011]'] == 'Si']

In [5]:
%%capture output

# Save the output as a variable that can be saved to a file
# Get the distribution of way of living
kind = data["D18"].value_counts(dropna=False)
print "Data:"
print kind
print ""
print "Data %:"
print data["D18"].value_counts(normalize=True,dropna=False) * 100

In [6]:
# Save+show the output to a text file
%save Q018-LivelloBando.py str(output)
shutil.move("Q018-LivelloBando.py", "text/Q018-LivelloBando.txt")


The following commands were written to file `Q018-LivelloBando.py`:
Data:
NaN          4
Provincia    3
Europa       2
Regione      1
dtype: int64

Data %:
NaN          40
Provincia    30
Europa       20
Regione      10
dtype: float64


In [7]:
# Swap nan for a more understandable word
old_dict = kind.to_dict()
new_dict = {}
for i in old_dict:
    if type(i) is float and np.isnan(i):
        new_dict["Nessuna risposta"] = old_dict[i]
    else:
        new_dict[i.capitalize()] = old_dict[i]

kindu = pd.Series(new_dict)
kind = kindu.order()

In [8]:
# Plot the data
plt.figure(figsize=(8,6))
plt.xlabel(u'Bando pubblico', fontsize=16)
plt.ylabel('Lab', fontsize=16)
plt.title(u"A che livello di istituzione pubblica era stato organizzato il bando?", fontsize=18, y=1.02)
my_colors = seaborn.color_palette("husl", len(kind)) # Set color palette
kind.plot(kind="bar",color=my_colors)
plt.savefig(u"svg/Q018-LivelloBando.svg")
plt.savefig(u"png/Q018-LivelloBando.png")
plt.savefig(u"pdf/Q018-LivelloBando.pdf")