Q027 - Quando è aperto al pubblico il 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]:
%%capture output

# Save the output as a variable that can be saved to a file
# For each subquestion, plot the data
subquestions = ["D27[SQ001_SQ001]","D27[SQ001_SQ002]","D27[SQ001_SQ003]","D27[SQ001_SQ004]",
                "D27[SQ001_SQ005]","D27[SQ001_SQ006]","D27[SQ001_SQ007]","D27[SQ002_SQ001]",
                "D27[SQ002_SQ002]","D27[SQ002_SQ003]","D27[SQ002_SQ004]","D27[SQ002_SQ005]",
                "D27[SQ002_SQ006]","D27[SQ002_SQ007]"]
subquestions_value = [u"Lunedì mattina",
            u"Martedì mattina",
            u"Mercoledì mattina",
            u"Giovedì mattina",
            u"Venerdì mattina",
            u"Sabato mattina",
            u"Domenica mattina",
            u"Lunedì pomeriggio",
            u"Martedì pomeriggio",
            u"Mercoledì pomeriggio",
            u"Giovedì pomeriggio",
            u"Venerdì pomeriggio",
            u"Sabato pomeriggio",
            u"Domenica pomeriggio",
]

plot_values = {}

for k,i in enumerate(subquestions):
    space = data[i].value_counts(dropna=False)
    print ""
    print "Data:",subquestions_value[k].encode('utf-8')
    print space
    print ""
    print "Data %:"
    print data[i].value_counts(normalize=True, dropna=False) * 100
    print ""
    print "Data: statistics:"
    print data[i].describe()
    plot_values[k] = []
    
    # Append Yes
    if 1 not in space.index:
        plot_values[k].append(0)
    else:
        plot_values[k].append(space[1])
    
    # Append No + NaN
    if 0 not in space.index and np.nan in space.index:
        plot_values[k].append(space[np.nan])
    elif 0 in space.index and np.nan not in space.index:
        plot_values[k].append(space[0])
    else:
        plot_values[k].append(space[0]+space[np.nan])

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


The following commands were written to file `Q027-AperturaLaboratorio.py`:

Data: Lunedì mattina
NaN    50
 1     17
 0      3
dtype: int64

Data %:
NaN    71.428571
 1     24.285714
 0      4.285714
dtype: float64

Data: statistics:
count    20.000000
mean      0.850000
std       0.366348
min       0.000000
25%       1.000000
50%       1.000000
75%       1.000000
max       1.000000
Name: D27[SQ001_SQ001], dtype: float64

Data: Martedì mattina
NaN    43
 1     24
 0      3
dtype: int64

Data %:
NaN    61.428571
 1     34.285714
 0      4.285714
dtype: float64

Data: statistics:
count    27.000000
mean      0.888889
std       0.320256
min       0.000000
25%       1.000000
50%       1.000000
75%       1.000000
max       1.000000
Name: D27[SQ001_SQ002], dtype: float64

Data: Mercoledì mattina
NaN    45
 1     23
 0      2
dtype: int64

Data %:
NaN    64.285714
 1     32.857143
 0      2.857143
dtype: float64

Data: statistics:
count    25.000000
mean      0.920000
std       0.276887
min       0.000000
25%       1.000000
50%       1.000000
75%       1.000000
max       1.000000
Name: D27[SQ001_SQ003], dtype: float64

Data: Giovedì mattina
NaN    44
 1     23
 0      3
dtype: int64

Data %:
NaN    62.857143
 1     32.857143
 0      4.285714
dtype: float64

Data: statistics:
count    26.000000
mean      0.884615
std       0.325813
min       0.000000
25%       1.000000
50%       1.000000
75%       1.000000
max       1.000000
Name: D27[SQ001_SQ004], dtype: float64

Data: Venerdì mattina
NaN    44
 1     24
 0      2
dtype: int64

Data %:
NaN    62.857143
 1     34.285714
 0      2.857143
dtype: float64

Data: statistics:
count    26.000000
mean      0.923077
std       0.271746
min       0.000000
25%       1.000000
50%       1.000000
75%       1.000000
max       1.000000
Name: D27[SQ001_SQ005], dtype: float64

Data: Sabato mattina
NaN    52
 1     18
dtype: int64

Data %:
NaN    74.285714
 1     25.714286
dtype: float64

Data: statistics:
count    18
mean      1
std       0
min       1
25%       1
50%       1
75%       1
max       1
Name: D27[SQ001_SQ006], dtype: float64

Data: Domenica mattina
NaN    64
 1      5
 0      1
dtype: int64

Data %:
NaN    91.428571
 1      7.142857
 0      1.428571
dtype: float64

Data: statistics:
count    6.000000
mean     0.833333
std      0.408248
min      0.000000
25%      1.000000
50%      1.000000
75%      1.000000
max      1.000000
Name: D27[SQ001_SQ007], dtype: float64

Data: Lunedì pomeriggio
NaN    42
 1     27
 0      1
dtype: int64

Data %:
NaN    60.000000
 1     38.571429
 0      1.428571
dtype: float64

Data: statistics:
count    28.000000
mean      0.964286
std       0.188982
min       0.000000
25%       1.000000
50%       1.000000
75%       1.000000
max       1.000000
Name: D27[SQ002_SQ001], dtype: float64

Data: Martedì pomeriggio
 1     41
NaN    29
dtype: int64

Data %:
 1     58.571429
NaN    41.428571
dtype: float64

Data: statistics:
count    41
mean      1
std       0
min       1
25%       1
50%       1
75%       1
max       1
Name: D27[SQ002_SQ002], dtype: float64

Data: Mercoledì pomeriggio
 1     36
NaN    34
dtype: int64

Data %:
 1     51.428571
NaN    48.571429
dtype: float64

Data: statistics:
count    36
mean      1
std       0
min       1
25%       1
50%       1
75%       1
max       1
Name: D27[SQ002_SQ003], dtype: float64

Data: Giovedì pomeriggio
 1     40
NaN    30
dtype: int64

Data %:
 1     57.142857
NaN    42.857143
dtype: float64

Data: statistics:
count    40
mean      1
std       0
min       1
25%       1
50%       1
75%       1
max       1
Name: D27[SQ002_SQ004], dtype: float64

Data: Venerdì pomeriggio
 1     35
NaN    35
dtype: int64

Data %:
 1     50
NaN    50
dtype: float64

Data: statistics:
count    35
mean      1
std       0
min       1
25%       1
50%       1
75%       1
max       1
Name: D27[SQ002_SQ005], dtype: float64

Data: Sabato pomeriggio
NaN    48
 1     22
dtype: int64

Data %:
NaN    68.571429
 1     31.428571
dtype: float64

Data: statistics:
count    22
mean      1
std       0
min       1
25%       1
50%       1
75%       1
max       1
Name: D27[SQ002_SQ006], dtype: float64

Data: Domenica pomeriggio
NaN    62
 1      8
dtype: int64

Data %:
NaN    88.571429
 1     11.428571
dtype: float64

Data: statistics:
count    8
mean     1
std      0
min      1
25%      1
50%      1
75%      1
max      1
Name: D27[SQ002_SQ007], dtype: float64


In [6]:
# Set the main figure
fig = plt.figure(figsize=(20,6))
fig.suptitle(u"Quando è aperto al pubblico il laboratorio?", fontsize=18, y=1.02)

# Set the subplots
l = 0
ax= []
rects = []
options = ["Si","No"]
for i in (range(7)):
    for k in (range(2)):
        # Create subplot
        ax.append(fig.add_subplot(2,7,l+1))
        ax[l].set_xlabel('Apertura')
        ax[l].set_ylabel('Lab')
        ax[l].set_title(subquestions_value[l])
        
        # Draw the bars
        plt.xticks(range(len(options)),options,rotation=0)
        ind = np.arange(len(options))   # the x locations for the groups
        width = 0.25                              # the width of the bars
        my_colors = seaborn.color_palette("Set1", len(options)) # Set color palette
        rect = ax[l].bar(ind,plot_values[l],width,color=my_colors[::-1],align='center')
        
        # Go to the next subplot
        l+=1

# Set the right margins
fig.tight_layout()

# Save the plot
plt.savefig(u"svg/Q027-AperturaLaboratorio.svg")
plt.savefig(u"png/Q027-AperturaLaboratorio.png")
plt.savefig(u"pdf/Q027-AperturaLaboratorio.pdf")



In [7]: