jmquintana79
3/28/2017 - 8:18 AM

Quantile forecast vs real data based on boxplots

Quantile forecast vs real data based on boxplots

import numpy as np
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(30,10))


## check if any number is multiple of..
def multiple(value, multiple):
    rest = value % multiple
    if rest == 0:
        return True
    else:
        return False

## data
lcolq = [ic for ic in DF.columns.values if 'q' in ic]
data = DF[lcolq].as_matrix()
data = np.transpose(data)
ldt = DF.index.tolist()
x = list(range(len(ldt)))
y = DF.real.tolist()
predictions = DF['q50'].values
# dates
lmut = np.array([multiple(idt.hour, 6) for idt in ldt])
lsdates = [idt.strftime("%Y-%m-%d %H") if imut else '' for imut,idt in zip(lmut,ldt)]
# match
lmatch = list(DF[['real','q5','q95']].apply(lambda x: True if x[0]>=float('%.1f'%x[1]) and x[0]<=float('%.1f'%x[2]) else False, axis=1))

# Plot box plot per each line point
boxplot_dict = ax.boxplot(data, positions=x, notch=False)

# Plot a line between the means of each dataset
plt.plot(x, y,linewidth=5,color="black",linestyle="dashed" ,label='real')

# format general labels in x axis
plt.xticks(x,fontsize=20,rotation='vertical')
ax.set_xticklabels(lsdates)
plt.yticks(fontsize=20,rotation='horizontal')           

# set limits
ax.set_ylim([0,1])

# set legend
ax.legend(loc='best',fontsize=22,shadow=True)

# set color of box plots
i=0
for b in boxplot_dict['boxes']:
    if lmatch[i]: b.set_color('green')
    else:b.set_color('red')
    i += 1

# title
plt.title('WP Forecast - Zone: %s \n [ %s - %s ]'%(zone,ldt[0],ldt[-1]),fontsize=18)

# Plot
plt.show()