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%"]
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")
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]: