In [1]:
import pandas as pd
from bokeh.plotting import output_notebook, show
output_notebook()
In [2]:
from bokeh.models import Plot, Patch, Patches, ColumnDataSource, Range1d, LinearAxis, HoverTool, TapTool
from bokeh.palettes import Spectral3, Spectral4
from bokeh._version import version_version
In [3]:
version_version
Out[3]:
In [4]:
patch_data = pd.DataFrame(
{
'oil' : [1, 2, 3, 1],
'gas' : [3, 2, 2, 3],
'solar': [0, 1, 1, 1]
},
index= [2010, 2011, 2012, 2013]
)
patch_data
Out[4]:
In [5]:
def make_patch_plot(xrange):
source = ColumnDataSource(patch_data)
plot = Plot(
title=None,
x_range=xrange, y_range=Range1d(0,5),
plot_width=300, plot_height=300,
min_border=0, toolbar_location=None)
for i, energy in enumerate(['gas', 'oil', 'solar']):
glyph = Patch(
x='index', y=energy,
fill_color=Spectral3[i], fill_alpha=0.5,
line_color='#CCC', line_alpha=0.5
)
plot.add_glyph(source, glyph)
return plot
In [6]:
asc = make_patch_plot(Range1d(2010, 2013))
asc.add_layout(LinearAxis(), 'below')
asc.add_layout(LinearAxis(), 'left')
asc.add_tools(HoverTool(tooltips=('hello world')))
asc.add_tools(TapTool())
show(asc)
In [7]:
desc = make_patch_plot(Range1d(2013, 2010))
show(desc)
In [8]:
patches_data = pd.DataFrame(
{
'xs' : [[1, 2, 3, 1], [2, 4, 6, 2], [3, 6, 9, 3], [1, -1, -2, 0] ],
'ys' : [[0, 1, 5, -1], [8, -3, 4, 0], [3, 4, 5, 4], [1, 4, 0, 2]],
'value': [Spectral4[0], Spectral4[1], Spectral4[2], Spectral4[3]]
},
)
patches_data
Out[8]:
In [9]:
def make_patches_plot(xrange, yrange):
source = ColumnDataSource(patches_data)
plot = Plot(
title=None,
x_range=xrange, y_range=yrange,
plot_width=300, plot_height=300,
min_border=0, toolbar_location=None)
glyph = Patches(xs='xs', ys='ys', fill_color='value', line_color='#CCC')
plot.add_glyph(source, glyph)
return plot
In [10]:
patches = make_patches_plot(Range1d(-5, 10), Range1d(-5,10))
patches.add_tools(HoverTool(tooltips='hello world'))
show(patches)
In [11]:
patches_rev = make_patches_plot(Range1d(10, -5), Range1d(-5, 10))
show(patches_rev)