Bokeh: Python: Cannot get HTML source for bar plots in Bokeh

戏子无情 提交于 2019-12-12 05:37:04

问题


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 bokeh.embed import autoload_static

plot = circle([1,2], [3,4])

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

jkl = HTML(div)
print div

However, if I try the same code with plot = Bar(...) I get the error:

-----> div = notebook_div(plot)
'Bar' object has no attribute 'ref'

Is there a better way to accomplish this, or is it simply not supported?


回答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


来源:https://stackoverflow.com/questions/27051734/bokeh-python-cannot-get-html-source-for-bar-plots-in-bokeh

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!