Q028 - Quali sono le dotazioni di macchinari di fabbricazione digitale del laboratorio?


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]:
# For each subquestion, plot the data
subquestions = ["D28[SQ001]","D28[SQ002]","D28[SQ003]",
                "D28[SQ004]","D28[SQ005]","D28[SQ006]",
                "D28[SQ007]","D28[SQ008]","D28[SQ009]",
                "D28[SQ010]","D28[SQ011]","D28[SQ012]",
                "D28[SQ013]","D28[SQ016]","D28[SQ014]",
                "D28[SQ015]"]
subquestions_value = [u"Fresatrice CNC di piccole dimensioni (indicativamente fino a 40cm)",
                      u"Fresatrice CNC di medie dimensioni (indicativamente fino a 1,50m)",
                      u"Fresatrice CNC di grandi dimensioni (indicativamente oltre 1,50m)",
                      u"Tornio CNC",
                      u"Taglio laser fino a 40 W di potenza",
                      u"Taglio laser oltre i 40 W di potenza",
                      u"Taglio vinile CNC",
                      u"Taglio al plasma",
                      u"Taglio a getto d’acqua",
                      u"Stampanti 3D: tecnologia FDM",
                      u"Stampanti 3D: stereolitografia (SLA, DLP)",
                      u"Stampanti 3D: a polvere",
                      u"Micro-controller (Arduino, Raspberry PI,..)",
                      u"Postazione di lavorazione elettronica (oscilloscopio, saldatore, ecc.)",
                      u"Macchina da taglio e cucito digitale",
                      u"Macchina da ricamo digitale"]

In [5]:
space = {}
for k,i in enumerate(subquestions):
    current_series = data[i].value_counts(dropna=False)
    old_dict = current_series.to_dict()
    new_dict = {}
    zero_value = 0.0
    nan_value = 0.0
    for i in old_dict.keys():
        if np.isnan(i):
            nan_value = old_dict[i]
        elif i == 0 or i == 0.0:
            zero_value = old_dict[i]
        else:
            new_dict[i] = old_dict[i]
    new_dict[0.0] = zero_value + nan_value
            
    gradou = pd.Series(new_dict)
    space[i] = gradou.order()

In [6]:
%%capture output

# Save the output as a variable that can be saved to a file
for k,i in enumerate(space):
    print ""
    print subquestions_value[k].encode('utf-8')
    print
    print "Data:"
    print space[i]
    print ""
    print "Data %:"
    print space[i] / space[i].sum() * 100
    print ""
    print "Data: statistics:"
    print space[i].describe()

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


The following commands were written to file `Q028-Macchinari.py`:

Fresatrice CNC di piccole dimensioni (indicativamente fino a 40cm)

Data:
1    10
0    60
dtype: int64

Data %:
1    14.285714
0    85.714286
dtype: float64

Data: statistics:
count     2.000000
mean     35.000000
std      35.355339
min      10.000000
25%      22.500000
50%      35.000000
75%      47.500000
max      60.000000
dtype: float64

Fresatrice CNC di medie dimensioni (indicativamente fino a 1,50m)

Data:
1     5
0    65
dtype: int64

Data %:
1     7.142857
0    92.857143
dtype: float64

Data: statistics:
count     2.000000
mean     35.000000
std      42.426407
min       5.000000
25%      20.000000
50%      35.000000
75%      50.000000
max      65.000000
dtype: float64

Fresatrice CNC di grandi dimensioni (indicativamente oltre 1,50m)

Data:
10     1
5      2
7      2
6      3
4      9
0     10
3     11
2     13
1     19
dtype: float64

Data %:
10     1.428571
5      2.857143
7      2.857143
6      4.285714
4     12.857143
0     14.285714
3     15.714286
2     18.571429
1     27.142857
dtype: float64

Data: statistics:
count     9.000000
mean      7.777778
std       6.180165
min       1.000000
25%       2.000000
50%       9.000000
75%      11.000000
max      19.000000
dtype: float64

Tornio CNC

Data:
12      1
40      1
50      1
100     1
2       2
4       2
6       2
3       3
30      3
1       5
20      7
5      11
10     13
0      18
dtype: float64

Data %:
12      1.428571
40      1.428571
50      1.428571
100     1.428571
2       2.857143
4       2.857143
6       2.857143
3       4.285714
30      4.285714
1       7.142857
20     10.000000
5      15.714286
10     18.571429
0      25.714286
dtype: float64

Data: statistics:
count    14.000000
mean      5.000000
std       5.349335
min       1.000000
25%       1.250000
50%       2.500000
75%       6.500000
max      18.000000
dtype: float64


In [8]:
# Swap nan for a more understandable word
space2 = {}
for i in space:
    old_dict = space[i].to_dict()
    new_dict = {}
    for k in old_dict:
        if isinstance(k, numpy.float64) and np.isnan(k):
            new_dict["Nessuna risposta"] = old_dict[k]
        elif type(k) is float and np.isnan(k):
            new_dict["Nessuna risposta"] = old_dict[k]
        else:
            new_dict[k] = old_dict[k]

    gradou = pd.Series(new_dict)
    space2[i] = gradou.order()

In [9]:
for k,i in enumerate(space2):
    # Plot the data 01
    plt.figure(figsize=(8,6))
    plt.xlabel(subquestions_value[k], fontsize=16)
    plt.ylabel('Lab', fontsize=16)
    plt.title(u"Quali sono le dotazioni di macchinari di fabbricazione digitale del laboratorio?", fontsize=18, y=1.02)
    my_colors = seaborn.color_palette("husl", len(space2[i])) # Set color palette
    space2[i].plot(kind="bar",color=my_colors)
    plt.savefig(u"svg/Q028-"+subquestions_value[k]+"01.svg")
    plt.savefig(u"png/Q028-"+subquestions_value[k]+"01.png")
    plt.savefig(u"pdf/Q028-"+subquestions_value[k]+"01.pdf")



In [10]:
# Plot the data 02
for k,i in enumerate(space2):
    # Reorder value_counts by index natural order
    space1 = space2[i].sort_index()

    plt.figure(figsize=(8,6))
    plt.title(u"Quali sono le dotazioni di macchinari di fabbricazione digitale del laboratorio? ", fontsize=18, y=1.02)
    plt.xlabel(subquestions_value[k], fontsize=16)
    plt.ylabel('Lab', fontsize=16)

    # Plot the data
    my_colors = seaborn.color_palette("husl", len(space1)) # Set color palette
    space1.plot(kind='bar',color=my_colors)
    plt.savefig(u"svg/Q028-"+subquestions_value[k]+"02.svg")
    plt.savefig(u"png/Q028-"+subquestions_value[k]+"02.png")
    plt.savefig(u"pdf/Q028-"+subquestions_value[k]+"02.pdf")



In [11]:
for k,i in enumerate(space2):
    # Check histogram
    plt.figure(figsize=(8,6))
    plt.title(u"Quali sono le dotazioni di macchinari di fabbricazione digitale del laboratorio?", fontsize=18, y=1.02)
    plt.xlabel(subquestions_value[k], fontsize=16)
    plt.ylabel('Lab', fontsize=16)
    space2[i].hist(bins=60)
    plt.savefig(u"svg/Q028-"+subquestions_value[k]+"03.svg")
    plt.savefig(u"png/Q028-"+subquestions_value[k]+"03.png")
    plt.savefig(u"pdf/Q028-"+subquestions_value[k]+"03.pdf")



In [11]: