In [1]:
import pandas as pd

from bokeh.plotting import output_notebook, show
output_notebook()


BokehJS successfully loaded.

In [2]:
from bokeh.models import Plot, Patch, Patches, Line, MultiLine, ColumnDataSource, Range1d, LinearAxis, HoverTool
from bokeh.palettes import Spectral3, Spectral4
from bokeh._version import version_version
from numpy import NAN

In [3]:
version_version


Out[3]:
'0.9.1dev.65161924'

Patch & Patches


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]:
gas oil solar
2010 3 1 0
2011 2 2 1
2012 2 3 1
2013 3 1 1

In [28]:
def make_patch_plot(xrange, data):
    source = ColumnDataSource(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
patch = make_patch_plot(Range1d(2010, 2013), patch_data)
show(patch)



In [29]:
def make_line_plot(xrange, data):
    source = ColumnDataSource(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 = Line(
            x='index', y=energy, 
            line_color=Spectral3[i], line_alpha=0.5, line_width=5
        )
        plot.add_glyph(source, glyph)
    
    return plot
line = make_line_plot(Range1d(2010, 2013), patch_data)
show(line)



In [7]:
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[7]:
value xs ys
0 #2b83ba [1, 2, 3, 1] [0, 1, 5, -1]
1 #abdda4 [2, 4, 6, 2] [8, -3, 4, 0]
2 #fdae61 [3, 6, 9, 3] [3, 4, 5, 4]
3 #d7191c [1, -1, -2, 0] [1, 4, 0, 2]

In [62]:
def make_patches_plot(xrange, yrange, data):
    source = ColumnDataSource(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', fill_alpha=0.8,
        line_color='#CCC', line_alpha=0.8)
    plot.add_glyph(source, glyph)
    return plot

patches = make_patches_plot(Range1d(-5, 10), Range1d(-5,10), patches_data)
show(patches)



In [70]:
def make_multiline_plot(xrange, yrange, data):
    source = ColumnDataSource(data)
    plot = Plot(
        title=None, 
        x_range=xrange, y_range=yrange,
        plot_width=300, plot_height=300,
        min_border=0, toolbar_location=None)

    glyph = MultiLine(xs='xs', ys='ys', line_color='value', line_width=5, line_alpha=0.8)
    plot.add_glyph(source, glyph)
    return plot

multiline = make_multiline_plot(Range1d(-5, 10), Range1d(-5,10), patches_data)
show(multiline)



In [71]:
patch_with_hole_data = pd.DataFrame(
    {
        'oil'  : [1, 2, 1, NAN, 3, 1, 2, 1],
        'gas'  : [3, 2, 2, NAN, 2, 3, 4, 1],
        'solar': [0, 1, 3, 2, NAN, 1, 4, 5]
    }, 
    index= [2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017]
)
patch_with_hole_data


Out[71]:
gas oil solar
2010 3 1 0
2011 2 2 1
2012 2 1 3
2013 NaN NaN 2
2014 2 3 NaN
2015 3 1 1
2016 4 2 4
2017 1 1 5

In [72]:
patch_with_hole = make_patch_plot(Range1d(2010, 2017), patch_with_hole_data)
show(patch_with_hole)



In [73]:
line_with_hole = make_line_plot(Range1d(2010, 2016), patch_with_hole_data)
show(line_with_hole)



In [80]:
patches_with_hole_data = pd.DataFrame(
    {
        'xs'  : [[1, 2, 3, NAN, 3, 1, 2], [2, 4, 5, NAN, 6, 2, 1], [3, 6, 8, NAN, 9, 3, 4], [1, -1, 2, NAN, 2, -2, 0] ],
        'ys'  : [[0, 1, -1, NAN, 5, -1, -2], [8, -3, 5, NAN, 4, 0, 1], [3, 4, 2, NAN, 5, 4, 6], [1, 4, 6, NAN, 1, 0, 2] ],
        'value': [Spectral4[0], Spectral4[1], Spectral4[2], Spectral4[3]]
    }, 
)

patches_with_hole_data


Out[80]:
value xs ys
0 #2b83ba [1, 2, 3, nan, 3, 1, 2] [0, 1, -1, nan, 5, -1, -2]
1 #abdda4 [2, 4, 5, nan, 6, 2, 1] [8, -3, 5, nan, 4, 0, 1]
2 #fdae61 [3, 6, 8, nan, 9, 3, 4] [3, 4, 2, nan, 5, 4, 6]
3 #d7191c [1, -1, 2, nan, 2, -2, 0] [1, 4, 6, nan, 1, 0, 2]

In [81]:
patches_with_hole = make_patches_plot(Range1d(-5, 10), Range1d(-5,10), patches_with_hole_data)
show(patches_with_hole)



In [82]:
multiline_with_holes = make_multiline_plot(Range1d(-5, 10), Range1d(-5,10), patches_with_hole_data)
show(multiline_with_holes)



In [ ]: