我已经安装了bokeh并从官方页面(http://bokeh.pydata.org/en/latest/docs/user_guide/server.html)下载了myapp.py,如下所示.
# myapp.py
from random import random
from bokeh.layouts import column
from bokeh.models import Button
from bokeh.palettes import RdYlBu3
from bokeh.plotting import figure, curdoc
# create a plot and style its properties
p = figure(x_range=(0, 100), y_range=(0, 100), toolbar_location=None)
p.border_fill_color = 'black'
p.background_fill_color = 'black'
p.outline_line_color = None
p.grid.grid_line_color = None
# add a text renderer to our plot (no data yet)
r = p.text(x=[], y=[], text=[], text_color=[], text_font_size="20pt",
text_baseline="middle", text_align="center")
i = 0
ds = r.data_source
# …Run Code Online (Sandbox Code Playgroud) 我是 Bokeh 和 Flask 的新手。我浏览了相关的问题、教程,并查看了 Bokeh 文档,但无法弄清楚我做错了什么。
话虽如此,我想创建一个简单的网络应用程序,在其中我将各种数据报告和图表“组合在一起”。
根据我阅读的内容,我想出了以下内容:
应用程序.py:
... # imports
app = Flask(__name__, static_url_path='/static')
@app.route("/")
def index():
return render_template("index.html")
@app.route("/bokeh_test")
def bokeh_test():
script, div = components(sample_plot())
return render_template("bokeh_test.html", script=script, div=div)
def sample_plot():
"""
A random plot just for testing purposes.
:return: plot
"""
PLOT_OPTIONS = dict(plot_width=600, plot_height=400)
SCATTER_OPTIONS = dict(size=12, alpha=0.5)
data = lambda: [random.choice([i for i in range(100)]) for r in range(10)]
plot = figure(sizing_mode='scale_both', tools='pan', **PLOT_OPTIONS)
plot.scatter(data(), data(), color="red", **SCATTER_OPTIONS)
# show(plot)
return plot
Run Code Online (Sandbox Code Playgroud)
散景测试.html: …
与悬停工具类似,我希望当我点击绘图的元素(在本例中为圆形)时,会弹出一个窗口,其中包含有关该元素的信息(见下图)。如果我在单击元素后移动鼠标,窗口将保持显示。Taptool 有工具提示吗?我非常努力地使用 CustomJS 来做到这一点,但无法显示 html 内容。有谁知道如何进行?
from bokeh.plotting import figure, output_file, show, ColumnDataSource
output_file("toolbar.html")
source = ColumnDataSource(data=dict(
x=[1, 2, 3, 4, 5],
y=[2, 5, 8, 2, 7],
desc=['A', 'b', 'C', 'd', 'E'],
imgs=[
'https://docs.bokeh.org/static/snake.jpg',
'https://docs.bokeh.org/static/snake2.png',
'https://docs.bokeh.org/static/snake3D.png',
'https://docs.bokeh.org/static/snake4_TheRevenge.png',
'https://docs.bokeh.org/static/snakebite.jpg'
],
fonts=[
'<i>italics</i>',
'<pre>pre</pre>',
'<b>bold</b>',
'<small>small</small>',
'<del>del</del>'
]
))
TOOLTIPS = """
<div>
<div>
<img
src="@imgs" height="42" alt="@imgs" width="42"
style="float: left; margin: 0px 15px 15px 0px;"
border="2"
></img>
</div>
<div>
<span style="font-size: 17px; font-weight: bold;">@desc</span>
<span style="font-size: 15px; color: #966;">[$index]</span> …Run Code Online (Sandbox Code Playgroud) 编辑:原始问题中的代码是指过期的Bokeh版本.但是下面的答案已经更新,以回答现代版Bokeh的相同问题
from bokeh.plotting import *
from bokeh.objects import *
output_notebook()
label = ['United States', 'Russia', 'South Africa', 'Europe (average)', 'Canada', 'Austalia', 'Japan']
number = [1, 2, 3, 4, 5, 6, 7]
value = [700, 530, 400, 150, 125, 125, 75]
yr = Range1d(start=0, end=800)
figure(y_range=yr)
rect(number, [x/2 for x in value] , width=0.5, height=value, color = "#ff1200")
show()
Run Code Online (Sandbox Code Playgroud)
我想用条形图中的区域标记条形Bokeh Plot.如何绘制带有类(名义或序数)的图表?请参阅示例http://en.wikipedia.org/wiki/File:Incarceration_Rates_Worldwide_ZP.svg.
注意:我正在使用Python v.2.7.6和IPython v.1.2.1.
如何为散景的步骤图示例中的线条添加图例:
https://docs.bokeh.org/en/latest/docs/reference/models/glyphs/step.html
我想在情节的“右上角”为每条线的颜色和线条样式添加图例。
该示例的默认代码是:
import numpy as np
from bokeh.models import ColumnDataSource, DataRange1d, Plot, LinearAxis, Grid
from bokeh.models.glyphs import Step
from bokeh.io import curdoc, show
N = 11
x = np.linspace(-2, 2, N)
y = x**2
source = ColumnDataSource(dict(x=x, y1=y, y2=y+2, y3=y+4))
xdr = DataRange1d()
ydr = DataRange1d()
plot = Plot(
title=None, x_range=xdr, y_range=ydr, plot_width=300, plot_height=300,
h_symmetry=False, v_symmetry=False, min_border=0,toolbar_location=None)
glyph1 = Step(x="x", y="y1", line_color="#f46d43", mode="before")
plot.add_glyph(source, glyph1)
glyph2 = Step(x="x", y="y2", line_dash="dashed", line_color="#1d91d0", mode="center")
plot.add_glyph(source, glyph2)
glyph3 = Step(x="x", y="y3", …Run Code Online (Sandbox Code Playgroud) 我刚刚将Python Bokeh升级到0.12.4我得到错误JSON对象有错误的类型字符串.我没有任何本地静态CDN库.这里有一些信息,但令人困惑.对于如何修复,有没有人有一个简单的答案?
注意:我的散景在Jupyter中运行良好.Flask web嵌入案例中失败了:
script, div = components(plot) return render_template('graph.html',
script=script, div=div)
Run Code Online (Sandbox Code Playgroud)
graph.html模板如下所示:
<link
href="http://cdn.bokeh.org/bokeh/release/bokeh-0.12.4.min.css"
rel="stylesheet" type="text/css">
<link
href="http://cdn.bokeh.org/bokeh/release/bokeh-widgets-0.12.4.min.css"
rel="stylesheet" type="text/css">
<script src="http://cdn.bokeh.org/bokeh/release/bokeh-0.12.4.min.js"></script>
<script src="http://cdn.bokeh.org/bokeh/release/bokeh-widgets-0.12.4.min.js"></script>
{{ script |safe }}
Run Code Online (Sandbox Code Playgroud) 如何在不更改应用程序中其他Divs的文本大小的情况下更改单个bokeh Div小部件内的文本大小?(https://docs.bokeh.org/en/latest/docs/reference/models/widgets.markups.html)
因此,我基本上遵循了散景文档站点上有关处理分类数据的示例:
https://docs.bokeh.org/en/latest/docs/user_guide/categorical.html
最终我得到了这段代码(我简化了一点):
# dictionary with data for making a figure
data = {'continents' : continents,
'2016' : list2016,
'2017' : list2017,
'2018' : list2018 }
source = ColumnDataSource(data=data)
p = figure(x_range=continents, y_range=(0, 450), plot_height=250, title="University count per continent per year",
toolbar_location=None, tools="")
p.vbar(x=dodge('continents', -0.25, range=p.x_range), top='2016', width=0.2, source=source,
color="#c9d9d3", legend=value("2016"))
p.vbar(x=dodge('continents', 0.0, range=p.x_range), top='2017', width=0.2, source=source,
color="#718dbf", legend=value("2017"))
p.vbar(x=dodge('continents', 0.25, range=p.x_range), top='2018', width=0.2, source=source,
color="#e84d60", legend=value("2018"))
p.x_range.range_padding = 0.1
p.xgrid.grid_line_color = None
p.legend.location = "top_right"
p.legend.orientation = "horizontal"
Run Code Online (Sandbox Code Playgroud)
其中数据列有 …
使用bokeh创建饼图时,如何从数据框中添加信息?
我正在使用http://docs.bokeh.org/en/latest/docs/gallery/pie_chart.html中的代码
基本上,我希望每个国家的价值观都在楔子中。
我尝试在十六进制地图中可视化我的数据。为此,我在图形类中使用了 python bokeh 和相应的 hex_tile 函数。我的数据属于 8 个不同类别之一,每个类别都有不同的颜色。下图显示了当前的可视化:
我想添加当鼠标悬停在元素上时更改元素(最好是所有类成员)颜色的可能性。
我知道,这在某种程度上是可能的,因为散景本身提供了以下示例:https : //docs.bokeh.org/en/latest/docs/gallery/hexbin.html
但是,我不知道如何自己实现(因为这似乎是 hexbin 函数的功能,而不是简单的 hex_tile 函数)
目前我在 ColumnDataSource 中提供我的数据:
source = ColumnDataSource(data=dict(
r=x_row,
q=y_col,
color=colors_array,
ipc_class=ipc_array
))
Run Code Online (Sandbox Code Playgroud)
其中“ipc_class”描述了元素所属的 8 个类之一。对于鼠标悬停工具提示,我使用了以下代码:
TOOLTIPS = [
("index", "$index"),
("(r,q)", "(@r, @q)"),
("ipc_class", "@ipc_class")
]
Run Code Online (Sandbox Code Playgroud)
然后我将所有内容可视化:
p = figure(plot_width=1600, plot_height=1000, title="Ipc to Hexes with colors", match_aspect=True,
tools="wheel_zoom,reset,pan", background_fill_color='#440154', tooltips=TOOLTIPS)
p.grid.visible = False
p.hex_tile('q', 'r', source=source, fill_color='color')
Run Code Online (Sandbox Code Playgroud)
我希望可视化添加一个函数,其中将鼠标悬停在一个元素上将导致以下之一: 1. 通过更改颜色突出显示当前元素 2. 通过更改其颜色来突出显示同一类的多个元素color 3. 元素悬停时更改hex_tile元素(或完整类)外线的颜色
散景可以实现这些功能中的哪些功能,我将如何实现?
编辑:在尝试重新实现 Tony 的建议后,只要我的鼠标点击图形,所有元素都会变成粉红色,并且颜色不会变回。我的代码如下所示:
source = ColumnDataSource(data=dict(
x=x_row,
y=y_col, …Run Code Online (Sandbox Code Playgroud)