In [1]:
import pandas as pd
from bokeh.plotting import show, output_notebook
output_notebook()
In [2]:
followers = pd.read_csv('followers-fake.csv', index_col='date', parse_dates=True)
followers.head()
Out[2]:
In [3]:
from bokeh.charts import TimeSeries
show(TimeSeries(followers, legend=True))
In [4]:
from bokeh.models import (
Plot, Line, LinearAxis, DatetimeAxis,
SingleIntervalTicker,
ColumnDataSource, Range1d,
Text, Circle, Legend
)
from datetime import datetime
xdr = Range1d(datetime(2015,1,1), datetime(2015,7,1))
ydr = Range1d(0, 1000)
plot = Plot(
x_range=xdr,
y_range=ydr,
title="",
plot_width=800,
plot_height=400,
outline_line_color=None,
toolbar_location=None,
)
AXIS_FORMATS = dict(
minor_tick_in=None,
minor_tick_out=None,
major_tick_in=None,
major_label_text_font_size="10pt",
major_label_text_font_style="normal",
axis_label_text_font_size="10pt",
axis_line_color='#AAAAAA',
major_tick_line_color='#AAAAAA',
major_label_text_color='#666666',
major_tick_line_cap="round",
axis_line_cap="round",
axis_line_width=1,
major_tick_line_width=1,
)
xaxis = DatetimeAxis(**AXIS_FORMATS)
yaxis = LinearAxis(SingleIntervalTicker(interval=200), axis_label="# of followers", **AXIS_FORMATS)
plot.add_layout(xaxis, 'below')
plot.add_layout(yaxis, 'left')
source = ColumnDataSource(followers)
party_list = list(followers.columns)
from bokeh.palettes import Spectral6
legends = []
for i, party in enumerate(party_list):
line = Line(x='date', y=party, line_color=Spectral6[i])
lg = plot.add_glyph(source, line)
legends.append((party, [lg]))
legend = Legend(legends=legends, orientation='top_left', border_line_color='white')
plot.add_layout(legend)
from bokeh.plotting import vplot, hplot
from bokeh.models import Callback
from bokeh.models.widgets import TextInput, Button
code="""
var startDate = start.get('value'),
endDate = end.get('value'),
start = new Date(startDate);
end = new Date(endDate);
dateRange.set('start', start.valueOf());
dateRange.set('end', end.valueOf());
"""
callback = Callback(args={}, code=code)
start = TextInput(callback=callback)
end = TextInput(callback=callback)
callback.args['start'] = start
callback.args['end'] = end
callback.args['dateRange'] = xdr
show(vplot(hplot(start, end), plot))