Skip to content Skip to sidebar Skip to footer

Bokeh: Python: Cannot Get Html Source For Bar Plots In Bokeh

I'm trying to retrieve html code for embedding from a Bokeh bar plot. This example works fine: from bokeh.resources import CDN from bokeh.plotting import circle from bo

Solution 1:

Bokeh.charts interface up to Bokeh version 0.7.0 provide a higher level of abstraction then plotting. It does not inherit from Plot thus cannot directly replace a plot instance. That said Chart types have an underlying plot object that can be used in this case. It is lazily created and at the moment needs some machinery to make it usable to you example. There are open discussions regarding Charts and it is very likely that this will going to be easier and more consistent in the releases.

In the meanwhile you can use the following approach (changing the bar notebook you can find in examples/charts):

from collections import OrderedDict
 import numpy as np
 from bokeh.charts import Bar
 from bokeh.sampledata.olympics2014 import data as original_data
 from IPython.core.display import HTML
 from bokeh.resources import CDN
 from bokeh.plotting import circle
 from bokeh.embed import autoload_static, notebook_div

 data = {d['abbr']: d['medals'] for d in original_data['data'] if d['medals']['total'] > 0}

 countries = sorted(data.keys(), key=lambda x: data[x]['total'], reverse=True)

 gold = np.array([data[abbr]['gold'] for abbr in countries], dtype=np.float)
 silver = np.array([data[abbr]['silver'] for abbr in countries], dtype=np.float)
 bronze = np.array([data[abbr]['bronze'] for abbr in countries], dtype=np.float)

 medals = OrderedDict(bronze=bronze, silver=silver, gold=gold)

 bar = Bar(medals, countries, title="grouped, dict_input", 
 xlabel="countries", ylabel="medals", legend=True, width=800, 
 height=600)
 bar.show()

 plot = bar.chart.plot
 div = notebook_div(plot)
 js, tag = autoload_static(plot, CDN, "some/path")

 jkl = HTML(div)
 print div

Post a Comment for "Bokeh: Python: Cannot Get Html Source For Bar Plots In Bokeh"