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]:
# 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",
]
In [5]:
plot_values = {}
for k,i in enumerate(subquestions):
space = data[i].value_counts(dropna=False)
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 [6]:
%%capture output
for k,i in enumerate(plot_values):
print ""
print "Data:",subquestions_value[k].encode('utf-8')
data_dict = {}
total_dict = 0.0
for j,l in enumerate(plot_values[i]):
data_dict[j] = plot_values[i][j]
total_dict += plot_values[i][j]
print "Aperto:",data_dict[0]
print "Chiuso:",data_dict[1]
print ""
print "Data %:"
print "Aperto:",data_dict[0]/total_dict* 100.0,"%"
print "Chiuso:",data_dict[1]/total_dict* 100.0,"%"
In [7]:
# Save+show the output to a text file
%save Q027-AperturaLaboratorio-Bis.py str(output)
shutil.move("Q027-AperturaLaboratorio-Bis.py", "text/Q027-AperturaLaboratorio-Bis.txt")
In [8]:
# Set the main figure
fig = plt.figure(figsize=(15,6))
fig.suptitle(u"Quando è aperto al pubblico il laboratorio?", fontsize=18, y=1.02)
# Reformat data
morning_yes = []
afternoon_yes = []
morning_no = []
afternoon_no = []
for k,i in enumerate(subquestions_value):
if k < len(subquestions_value)/2:
morning_yes.append(plot_values[k][0])
morning_no.append(plot_values[k][1])
else:
afternoon_yes.append(plot_values[k][0])
afternoon_no.append(plot_values[k][1])
# Set the subplots
ax= []
rects = []
options = ["Si","No"]
plots = ["Mattina","Pomeriggio"]
days = [u"Lunedì",u"Martedì",u"Mercoledí",u"Giovedì",u"Venerdì",u"Sabato",u"Domenica"]
for k,i in enumerate((range(2))):
# Create subplot
ax.append(fig.add_subplot(2,1,k+1))
ax[k].set_xlabel('Apertura')
ax[k].set_ylabel('Lab')
ax[k].set_title(plots[k])
# Draw the bars
plt.xticks(range(len(days)),days,rotation=0)
ind = np.arange(len(days)) # the x locations for the groups
width = 0.25 # the width of the bars
my_colors = seaborn.color_palette("Set1", 2) # Set color palette
if k == 0:
toplot1 = morning_yes
toplot2 = morning_no
else:
toplot1 = afternoon_yes
toplot2 = afternoon_no
rect1 = ax[k].bar(ind,toplot1,width,color=my_colors[1],align='center') # Plot Yes
rect2 = ax[k].bar(ind+width,toplot2,width,color=my_colors[0],align='center') # Plot No
plt.legend( (rect1, rect2), ('Aperto', 'Chiuso') )
# Set the right margins
fig.tight_layout()
# Save the plot
plt.savefig(u"svg/Q027-AperturaLaboratorio-Bis.svg")
plt.savefig(u"png/Q027-AperturaLaboratorio-Bis.png")
plt.savefig(u"pdf/Q027-AperturaLaboratorio-Bis.pdf")
In [8]: