Q040 - Rispetto alla tua esperienza di produttore, in percentuale quanto della tua attività è collegata a:


In [5]:
# -*- 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 [6]:
# Load csv file first
data = pd.read_csv("data/results-makers-40.csv", encoding="utf-8")

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

In [16]:
# Range: Q040[SQ001] - Q040[SQ003]

agree_columns = ['Q040[SQ001]','Q040[SQ002]','Q040[SQ003]']
agree_options = ["Fabbricazione digitale",
                 "Fabbricazione analogica",
                 "Altro"]
agree = data[agree_columns]
agree.replace(np.nan, 'Nessuna risposta', inplace=True) # Simplify text 
agree_types = ["Nessuna risposta","10%","20%","30%","40%","50%","60%","70%","80%","90%","100%"]


-c:8: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame

In [9]:
#agree[0:4]

In [17]:
%%capture output

# Save the output as a variable that can be saved to a file
# Gather data
agree_b = {}
for k,i in enumerate(agree_columns):
    agree_b[k] = agree[i].value_counts()
    print "Data:",agree_options[k]
    print agree_b[k]
    print
    print "Data %:",agree_options[k]
    print agree[i].value_counts(normalize=True)*100
    print

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


The following commands were written to file `Q040-TipoFabbricazione.py`:
Data: Fabbricazione digitale
Nessuna risposta    30
80%                 15
50%                 14
10%                 13
20%                 13
70%                 10
30%                  9
100%                 8
90%                  8
40%                  8
60%                  6
dtype: int64

Data %: Fabbricazione digitale
Nessuna risposta    22.388060
80%                 11.194030
50%                 10.447761
10%                  9.701493
20%                  9.701493
70%                  7.462687
30%                  6.716418
100%                 5.970149
90%                  5.970149
40%                  5.970149
60%                  4.477612
dtype: float64

Data: Fabbricazione analogica
Nessuna risposta    23
50%                 21
10%                 16
80%                 13
20%                 13
100%                12
90%                 11
40%                 10
60%                  9
30%                  4
70%                  2
dtype: int64

Data %: Fabbricazione analogica
Nessuna risposta    17.164179
50%                 15.671642
10%                 11.940299
80%                  9.701493
20%                  9.701493
100%                 8.955224
90%                  8.208955
40%                  7.462687
60%                  6.716418
30%                  2.985075
70%                  1.492537
dtype: float64

Data: Altro
Nessuna risposta    93
10%                 11
50%                  7
20%                  7
40%                  4
60%                  4
30%                  2
100%                 2
70%                  2
80%                  1
90%                  1
dtype: int64

Data %: Altro
Nessuna risposta    69.402985
10%                  8.208955
50%                  5.223881
20%                  5.223881
40%                  2.985075
60%                  2.985075
30%                  1.492537
100%                 1.492537
70%                  1.492537
80%                  0.746269
90%                  0.746269
dtype: float64



In [19]:
p10 = []
p20 = []
p30 = []
p40 = []
p50 = []
p60 = []
p70 = []
p80 = []
p90 = []
p100 = []
nanvalue = []

for k,i in enumerate(agree_columns):
    agree_presents = agree_b[k].index.tolist()
    
    # Convert NaN to "NaN"
    #for o,h in enumerate(agree_presents):
    #    if type(h) is float:
    #        agree_presents.pop(o)
    #        agree_presents.append("NaN")
    
    # Reassign new list with "NaN"
    agree_b[k].index = agree_presents
    
    # Check for empty values, and put a 0 instead
    if "10%" not in agree_presents:
        p10.append(0)
    if "20%" not in agree_presents:
        p20.append(0)
    if "30%" not in agree_presents:
        p30.append(0)
    if "40%" not in agree_presents:
        p40.append(0)
    if "50%" not in agree_presents:
        p50.append(0)
    if "60%" not in agree_presents:
        p60.append(0)
    if "70%" not in agree_presents:
        p70.append(0)
    if "80%" not in agree_presents:
        p80.append(0)
    if "90%" not in agree_presents:
        p90.append(0)
    if "100%" not in agree_presents:
        p100.append(0)
    if "Nessuna risposta" not in agree_presents:
        nanvalue.append(0)
        
    for j in agree_presents:
        if j == "10%":
            p10.append(agree_b[k].ix["10%"])
        elif j == "20%":
            p20.append(agree_b[k].ix["20%"])
        elif j == "30%":
            p30.append(agree_b[k].ix["30%"])
        elif j == "40%":
            p40.append(agree_b[k].ix["40%"])
        elif j == "50%":
            p50.append(agree_b[k].ix["50%"])
        elif j == "60%":
            p60.append(agree_b[k].ix["60%"])
        elif j == "70%":
            p70.append(agree_b[k].ix["70%"])
        elif j == "80%":
            p80.append(agree_b[k].ix["80%"])
        elif j == "90%":
            p90.append(agree_b[k].ix["90%"])
        elif j == "100%":
            p100.append(agree_b[k].ix["100%"])
        elif j == "Nessuna risposta":
            nanvalue.append(agree_b[k].ix[j])

In [20]:
# Plot the data
plt.figure(figsize=(14,6))
plt.xlabel('Tipo di fabbricazione', fontsize=16)
plt.ylabel('Persone', fontsize=16)
plt.title(u'Rispetto alla tua esperienza di produttore, in percentuale quanto della tua attività è collegata a:', fontsize=18, y=1.02)
plt.xticks(range(len(agree_options)+1),agree_options,rotation=0)
ind = np.arange(len(agree_columns))   # the x locations for the groups
width = 0.07                             # the width of the bars

my_colors = seaborn.dark_palette("skyblue", 11, reverse=True) # Set color palette
nan_color = seaborn.color_palette("Set1", 1) # Set color palette
rect1 = plt.bar(ind,p10,width,color=my_colors[0],align='center') # Plot 10%
rect2 = plt.bar(ind+width,p20,width,color=my_colors[1],align='center') # Plot 20% 
rect3 = plt.bar(ind+width*2,p30,width,color=my_colors[2],align='center') # Plot 30%
rect4 = plt.bar(ind+width*3,p40,width,color=my_colors[3],align='center') # Plot 40%
rect5 = plt.bar(ind+width*4,p50,width,color=my_colors[4],align='center') # Plot 50%
rect6 = plt.bar(ind+width*5,p60,width,color=my_colors[5],align='center') # Plot 60%
rect7 = plt.bar(ind+width*6,p70,width,color=my_colors[6],align='center') # Plot 70%
rect8 = plt.bar(ind+width*7,p80,width,color=my_colors[7],align='center') # Plot 80%
rect9 = plt.bar(ind+width*8,p90,width,color=my_colors[8],align='center') # Plot 90%
rect10 = plt.bar(ind+width*9,p100,width,color=my_colors[9],align='center') # Plot 100%
rect11 = plt.bar(ind+width*10,nanvalue,width,color=nan_color,align='center') # Plot NaN
plt.legend( (rect1, rect2, rect3, rect4, rect5, rect6, rect7, rect8, rect9, rect10, rect11), 
           ('10%', '20%', '30%', '40%', '50%', '60%', '70%', '80%', '90%', '100%', 'NaN') )
plt.savefig("svg/Q040-TipoFabbricazione.svg")
plt.savefig("png/Q040-TipoFabbricazione.png")
plt.savefig("pdf/Q040-TipoFabbricazione.pdf")



In [9]: