散景:Python:无法获取散景中条形图的HTML源 [英] Bokeh: Python: Cannot get HTML source for bar plots in Bokeh

查看:71
本文介绍了散景:Python:无法获取散景中条形图的HTML源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从bokeh条形图中检索要嵌入的html代码.

I'm trying to retrieve html code for embedding from a Bokeh bar plot.

这个例子很好用:从bokeh.resources导入CDN来自bokeh.plotting导入圈子从bokeh.embed导入autoload_static

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

但是,如果我使用plot = Bar(...)尝试相同的代码,则会收到错误消息:

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?

推荐答案

Bokeh.charts接口(直至Bokeh版本0.7.0)提供了更高的抽象级别,然后进行了绘制.它不从Plot继承,因此不能直接替换Plot实例.所说的图表类型具有在这种情况下可以使用的基础绘图对象.它是惰性创建的,目前需要一些机制使其可用于您的示例.有关Charts的公开讨论,这很可能会在发行版中变得更加容易和更加一致.

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

这篇关于散景:Python:无法获取散景中条形图的HTML源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆