import plotly.plotly as py
from plotly.graph_objs import *
import plotly.tools as tls
def make_Bars( x, y, color):
return Bar(
x=x,
y=y,
marker=Marker(
color=color),
name='',
)
axis_style = dict(
showticklabels=True,
showgrid=False,
zeroline=False,
mirror=False)
def make_XAxis():
xaxis = XAxis()
xaxis.update(axis_style, tickangle=-45, showline=True, tickfont=Font(size=10))
return xaxis
def make_YAxis():
yaxis = YAxis()
yaxis.update(axis_style, range=[0,88.2], title='%', showgrid=True,
gridwidth=1,
gridcolor='#FFFFFF')
return yaxis
The gradient of colors for democrats and republicans:
dem_colors=['#000096', '#404ca0', '#6175c1', '#758bd1']
rep_colors=['#8B0000', '#9E051B', '#B0122C', '#C0223B',
'#CF3447', '#DB4551', '#E75758', '#F06A5E',
'#F87D64', '#FE906A', '#FFA474']
titles=('PredictWise Chance of Winning<br> Democratic Nomination,<br> September 17, 2015',
'PredictWise Chance of Winning Republican Nomination, <br> September 17, 2015',
'October 4, 2015', 'October 4, 2015',
'October 18, 2015', 'October 18, 2015',
'October 29, 2015', 'October 29, 2015')
figure = tls.make_subplots(rows=4, cols=2, shared_yaxes=True, subplot_titles=titles,
horizontal_spacing=0.15,
vertical_spacing=0.1,
print_grid=True)
This is the format of your plot grid: [ (1,1) x1,y1 ] [ (1,2) x2,y1 ] [ (2,1) x3,y2 ] [ (2,2) x4,y2 ] [ (3,1) x5,y3 ] [ (3,2) x6,y3 ] [ (4,1) x7,y4 ] [ (4,2) x8,y4 ]
Uncomment the next line, and run the cell to see how the initial layout is set. Then you can update the layout for each subplot.
#print figure['layout']
pl_width=775
pl_height=950
title = 'Political Prediction Markets. Who will win the nomination for presidential election?'
figure['layout'].update(title=title,
font= Font(family="Open Sans, sans-serif"),
showlegend=False,
hovermode='x',
autosize=False,
width=pl_width,
height=pl_height,
plot_bgcolor='#EFECEA',
bargap=0.05,
margin=Margin(
l=65,
r=65,
b=85,
t=150
)
)
dem_cand=['H Clinton', 'B Sanders', 'J Biden', "O'Malley"]
rep_cand=['J Bush', 'D Trump', 'M Rubio', 'B Carson', 'S Walter',
'J Kasich', ' C Fiorina', 'T Cruz', 'M Huckabee', 'C Christie', 'R Paul']
dem_predict=[[70, 14, 13, 1], [69, 12, 17,0], [77, 11, 10, 0], [88, 12, 0, 0]]
rep_predict=[[36, 16, 13, 9, 8, 5, 4, 3, 3, 1, 1],
[31, 13, 29, 6, 0, 2, 6, 4, 2, 5, 1],
[28, 12, 31, 9, 0, 2, 5, 5, 2, 4, 1],
[11,20,41,11,0,2,2,7,1,4,0]]
Bars=[]
for k in range(4):
Bars.append( [make_Bars(dem_cand, dem_predict[k], dem_colors[1]),
make_Bars(rep_cand, rep_predict[k], rep_colors[2])] )
figure.append_trace(Bars[0][0], 1, 1)
figure.append_trace(Bars[0][1], 1, 2)
figure.append_trace(Bars[1][0], 2, 1)
figure.append_trace(Bars[1][1], 2, 2)
figure.append_trace(Bars[2][0], 3, 1)
figure.append_trace(Bars[2][1], 3, 2)
figure.append_trace(Bars[3][0], 4, 1)
figure.append_trace(Bars[3][1], 4, 2)
for s in range(1,9):
figure['layout'].update({'xaxis{}'.format(s): make_XAxis()})# set xaxis style
for s in [1, 2, 3, 4]:
figure['layout'].update({'yaxis{}'.format(s): make_YAxis()})# set the sharey axis style
anno_text="Data source:\
<a href='http://www.predictwise.com/'> [1]</a>."
figure['layout']['annotations']+=[
Annotation(
showarrow=False,
text=anno_text,
xref='paper',
yref='paper',
x=0,
y=-0.12,
xanchor='left',
yanchor='bottom',
font=Font(
size=11 )
)
]
for s in range(8): #change the default font size (of 16) for subplots title
figure['layout']['annotations'][s]['font']= {'size': 12}
Update the width of the subplots:
for s in [1, 3, 5, 7]:
figure['layout']['xaxis{}'.format(s)].update({'domain': [0.0, 0.2516]})
for s in [2, 4, 6, 8]:
figure['layout']['xaxis{}'.format(s)].update({'domain': [0.2816, 1.0]})
Update 'x'- coordinate for the subplot titles:
for s in [0, 2, 4, 6]:
figure['layout']['annotations'][s].update({'x': 0.14})# set the starting 'x' for subplot 1, 3, title
for s in [1, 3, 5, 7]:
figure['layout']['annotations'][s].update({'x': 0.6})# for subplot 2, 4 title
#py.sign_in('empet', 'my_api_key')
#py.plot(figure, filename='Prediction-Nomination')
import plotly
plotly.offline.init_notebook_mode()
plotly.offline.iplot(figure)
from IPython.core.display import HTML
def css_styling():
styles = open("./custom.css", "r").read()
return HTML(styles)
css_styling()