In [1]:
# -*- coding: UTF-8 -*-
# Render our plots inline
%matplotlib inline
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib
import matplotlib.lines as mlines
import numpy as np
import seaborn
import math
import datetime as dt
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'})
rcParams['figure.figsize'] = 10, 20
In [2]:
# Load csv file first
data = pd.read_csv("data/lab-survey.csv", encoding="utf-8")
In [3]:
history_columns = ['D1','D12','D13']
history = data[history_columns]
In [4]:
rows = []
for i in range(len(data.index)):
rows.append(i)
history["Row"] = pd.Series(rows)
In [5]:
len(history)
Out[5]:
In [6]:
labs = list(history["D1"].values.tolist())
# Get min and max values, and extend the time range by 60 days
start1 = pd.to_datetime(history["D12"][history["D12"].notnull()].min())
dStart = start1.to_datetime()
dStart -= datetime.timedelta(days=60)
end1 = pd.to_datetime(history["D13"][history["D13"].notnull()].max())
dEnd = end1.to_datetime()
dEnd += datetime.timedelta(days=60)
In [21]:
# From http://stackoverflow.com/questions/7684475/plotting-labeled-intervals-in-matplotlib-gnuplot
from matplotlib.dates import DateFormatter, MinuteLocator, SecondLocator
import datetime as dt
from pylab import rcParams
# Setup the plot
fig = plt.figure(figsize=(12,9))
# Timeline function
def timelines(y, xstart, xstop, color='b', style="-"):
plt.hlines(y, xstart, xstop, color, lw=3, linestyles=style)
plt.vlines(xstart, y+0.3, y-0.3, color, lw=2)
plt.vlines(xstop, y+0.3, y-0.3, color, lw=2)
#Setup the plot
ax = fig.gca()
ax.xaxis_date()
myFmt = DateFormatter('%Y-%m-%d')
ax.xaxis.set_major_formatter(myFmt)
ax.yaxis.set_ticks(range(len(labs[0:35])))
ax.yaxis.set_ticklabels(labs[0:35])
plt.xlim(dStart,dEnd)
plt.ylim(-1,len(labs[0:35])+4) # Give extra space for legend at the top and for the first value at the bottom
plt.xlabel('Periodo', fontsize=16)
plt.ylabel('Lab', fontsize=16)
plt.title('Periodo di sviluppo dei laboratori', fontsize=18, y=1.02)
# Plot all the timelines
for i in history.index[0:35]:
# Check if the lab is closing...
if data["D3"][i] == "in chiusura":
color = "y"
else:
color = "g"
start = pd.to_datetime(history["D12"][i])
end = pd.to_datetime(history["D13"][i])
# Debug
#print history["D1"][i],start,end
if type(history["D12"][i]) == float and math.isnan(history["D12"][i]):
# There is no start, i.e. no design process, only inauguration
start = end
timelines(history["Row"][i], start, end, 'c', style="--")
timelines(history["Row"][i], end, dEnd, color, style="-")
elif type(history["D13"][i]) == float and math.isnan(history["D13"][i]):
# There is no inauguration yet
timelines(history["Row"][i], start, dEnd, 'c', style="--")
else:
timelines(history["Row"][i], start, end, 'c', style="--")
timelines(history["Row"][i], end, dEnd, color, style="-")
# Setup legend
design_line = mlines.Line2D([], [], color='c', ls="--", lw=3, label='Progettazione')
open_line = mlines.Line2D([], [], color='g', ls="-", lw=3, label='Aperto')
closing_line = mlines.Line2D([], [], color='y', ls="-", lw=3, label='Aperto ma in chiusura')
plt.legend(handles=[design_line,open_line,closing_line])
# Create, save, show the plot
plt.savefig("pdf/Q012-13-PeriodoDiSviluppo01.pdf")
plt.show()
In [23]:
# From http://stackoverflow.com/questions/7684475/plotting-labeled-intervals-in-matplotlib-gnuplot
from matplotlib.dates import DateFormatter, MinuteLocator, SecondLocator
import datetime as dt
from pylab import rcParams
# Setup the plot
fig = plt.figure(figsize=(12,9))
# Timeline function
def timelines(y, xstart, xstop, color='b', style="-"):
plt.hlines(y, xstart, xstop, color, lw=3, linestyles=style)
plt.vlines(xstart, y+0.3, y-0.3, color, lw=2)
plt.vlines(xstop, y+0.3, y-0.3, color, lw=2)
#Setup the plot
ax = fig.gca()
ax.xaxis_date()
myFmt = DateFormatter('%Y-%m-%d')
ax.xaxis.set_major_formatter(myFmt)
ax.yaxis.set_ticks(range(len(labs[36:70])))
ax.yaxis.set_ticklabels(labs[36:70])
plt.xlim(dStart,dEnd)
plt.ylim(-1,len(labs[36:70])+4) # Give extra space for legend at the top and for the first value at the bottom
plt.xlabel('Periodo', fontsize=16)
plt.ylabel('Lab', fontsize=16)
plt.title('Periodo di sviluppo dei laboratori', fontsize=18, y=1.02)
# Plot all the timelines
for i in history.index[36:70]:
# Check if the lab is closing...
if data["D3"][i] == "in chiusura":
color = "y"
else:
color = "g"
start = pd.to_datetime(history["D12"][i])
end = pd.to_datetime(history["D13"][i])
# Debug
#print history["D1"][i],start,end
if type(history["D12"][i]) == float and math.isnan(history["D12"][i]):
# There is no start, i.e. no design process, only inauguration
start = end
timelines(history["Row"][i]-36, start, end, 'c', style="--")
timelines(history["Row"][i]-36, end, dEnd, color, style="-")
elif type(history["D13"][i]) == float and math.isnan(history["D13"][i]):
# There is no inauguration yet
timelines(history["Row"][i]-36, start, dEnd, 'c', style="--")
else:
timelines(history["Row"][i]-36, start, end, 'c', style="--")
timelines(history["Row"][i]-36, end, dEnd, color, style="-")
# Setup legend
design_line = mlines.Line2D([], [], color='c', ls="--", lw=3, label='Progettazione')
open_line = mlines.Line2D([], [], color='g', ls="-", lw=3, label='Aperto')
closing_line = mlines.Line2D([], [], color='y', ls="-", lw=3, label='Aperto ma in chiusura')
plt.legend(handles=[design_line,open_line,closing_line])
# Create, save, show the plot
plt.savefig("pdf/Q012-13-PeriodoDiSviluppo02.pdf")
plt.show()
In [8]: