Q011 - Come vivi?


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/results-makers-40.csv", encoding="utf-8")

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

In [4]:
%%capture output

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

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


The following commands were written to file `Q011-MakerComeVivi01.py`:
Data:
In coppia              41
In coppia con figli    29
Da solo                20
Con i genitori         19
NaN                    10
Con amici               9
Altro                   6
dtype: int64

Data %:
In coppia              30.597015
In coppia con figli    21.641791
Da solo                14.925373
Con i genitori         14.179104
NaN                     7.462687
Con amici               6.716418
Altro                   4.477612
dtype: float64


In [6]:
# Plot the data
plt.figure(figsize=(8,6))
plt.xlabel(u'Modalitá', fontsize=16)
plt.ylabel('Persone', fontsize=16)
plt.title("Come vivi?", fontsize=18, y=1.02)
my_colors = seaborn.color_palette("husl", len(living)) # Set color palette
living.plot(kind="bar",color=my_colors)
plt.savefig("svg/Q011-MakerComeVivi01.svg")
plt.savefig("png/Q011-MakerComeVivi01.png")
plt.savefig("pdf/Q011-MakerComeVivi01.pdf")


/usr/local/lib/python2.7/site-packages/matplotlib/font_manager.py:1279: UserWarning: findfont: Font family ['Source Sans Pro'] not found. Falling back to Bitstream Vera Sans
  (prop.get_family(), self.defaultFamily[fontext]))

In [7]:
%%capture output

# Save the output as a variable that can be saved to a file
# Get "other" data
living_other = data["Q011[other]"].str.lower().value_counts()
print "Data:"
print living_other
print ""
print "Data %:"
print data["Q011[other]"].str.lower().value_counts(normalize=True) * 100

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


The following commands were written to file `Q011-MakerProfilo.py`:
Data:
da solo con figlio                       1
sola con un bimbo in affido condiviso    1
con i miei figli                         1
con mio padre.                           1
da sola con un bambino                   1
un po da solo un po con i genitori       1
dtype: int64

Data %:
da solo con figlio                       0.746269
sola con un bimbo in affido condiviso    0.746269
con i miei figli                         0.746269
con mio padre.                           0.746269
da sola con un bambino                   0.746269
un po da solo un po con i genitori       0.746269
dtype: float64


In [9]:
# Plot bar
plt.figure(figsize=(8,6))
plt.title('Come vivi? Altro', fontsize=18, y=1.02)
plt.xticks(range(len(living_other.index)),living_other.index,rotation=90)
plt.xlabel(u'Modalitá', fontsize=16)
plt.ylabel('Persone', fontsize=16)
ind = np.arange(len(living_other))   # the x locations for the groups
width = 0.35                         # the width of the bars

my_colors = seaborn.color_palette("husl", len(living_other)) # Set color palette
rect1 = plt.bar(ind,living_other,width,color=my_colors,align='center')
plt.savefig("svg/Q011-MakerComeVivi02.svg")
plt.savefig("png/Q011-MakerComeVivi02.png")
plt.savefig("pdf/Q011-MakerComeVivi02.pdf")


Merge relevant data


In [10]:
living["Da solo con figli"] = 4
living["Con i genitori"] += 1
living["Altro"] -= 5

In [11]:
%%capture output

# Save the output as a variable that can be saved to a file
print "Data:"
print living
print ""
print "Data %:"
print living/living.sum()

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


The following commands were written to file `Q011-MakerProfilo01.py`:
Data:
In coppia              41
In coppia con figli    29
Da solo                20
Con i genitori         20
NaN                    10
Con amici               9
Altro                   1
Da solo con figli       4
dtype: int64

Data %:
In coppia              0.305970
In coppia con figli    0.216418
Da solo                0.149254
Con i genitori         0.149254
NaN                    0.074627
Con amici              0.067164
Altro                  0.007463
Da solo con figli      0.029851
dtype: float64


In [13]:
# Plot the data
plt.figure(figsize=(8,6))
plt.xlabel(u'Modalitá', fontsize=16)
plt.ylabel('Persone', fontsize=16)
plt.title("Come vivi?", fontsize=18, y=1.02)
my_colors = seaborn.color_palette("husl", len(living)) # Set color palette
living.plot(kind="bar",color=my_colors)
plt.savefig("svg/Q011-MakerComeVivi03.svg")
plt.savefig("png/Q011-MakerComeVivi03.png")
plt.savefig("pdf/Q011-MakerComeVivi03.pdf")