import numpy as np
from scipy.ndimage import imread
import pylab as pl
names = ['N-VA','PS','MR','CD&V','OVLD','SP-A','CDH','Groen','Ecolo','VB','PTB-GO!','FDF','PP','Sonstige']
votes = [20.26,11.67,9.64,11.61,9.78,8.83,4.98,5.32,3.30,3.67,1.97,1.80,1.52,5.65]
seats = [33,23,20,18,14,13,9,6,6,3,2,2,1]
colors = ['#fec44f', '#b30000', '#2b8cbe', '#e06b13', '#0868ac', '#c22c1c', '#ec7014', '#a6d96a', '#1a9850', '#8c510a', '#B30000', '#de007b', '#605e8b', '#b3b3b3']
z = sorted(list(zip(names,votes,colors))[:-1], key = lambda x: x[1],reverse = True)
names2 = [x[0] for x in z] + [names[-1]]
votes2 = [x[1] for x in z] + [votes[-1]]
colors2 = [x[2] for x in z] + [colors[-1]]
fig = pl.figure(1,figsize = (4.446 , 8.092))
artists = pl.barh(range(len(names2),0,-1), votes2)
for bar,color,name,value in zip(artists.get_children(),colors2,names2,votes2):
bar.set_facecolor(color)
bar.set_linewidth(0)
bb = bar.get_bbox()
x = bb.x1 + 1
y = bb.y0 #+0.5*(bb.y0-bb.y1)
pl.text(x,y, '%s\n%0.1f' % (name,value) + '%', fontsize=12)
pl.xticks([])
pl.yticks([])
pl.ylim(0.75,15)
ax = pl.gca()
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['left'].set_linewidth(3)
pl.savefig('votes.svg')
pl.figure(2)
ax = pl.subplot(1,1,1)
ax.set_aspect(1)
wedges, texts = ax.pie(0.5*np.array(seats)/np.sum(seats), counterclock=False, startangle=180, colors=colors[:-1], wedgeprops = { 'linewidth' : 0 }, labels = ['%s\n%d'%(n,s) for n,s in zip(names[:-1],seats)], labeldistance = 1.2)
for t in texts:
t.set_horizontalalignment('center')
lw = 2
#draw line around pie
ax.pie([0.5], counterclock=False, startangle=180, colors=['#ffffff'], wedgeprops = { 'linewidth' : lw,'fill':False })
#draw a circle at the center of pie to make it look like a donut
centre_circle = pl.Circle((0,0),0.34,color='black', fc='white',linewidth=lw)
ax.add_artist(centre_circle)
rect = pl.Rectangle((-1,-0.5),2,0.49, facecolor='white', linewidth=0)
#centre_circle.set_clip_path(rect)
ax.add_artist(rect)
pl.savefig('seats.svg')