散景:在 push_notebook() 之前更改散点图的数据
Bokeh: chage data of Scatter plot before push_notebook()
我的想法是使用提供的交互器示例 in this sample repo 但是使用 Bokeh 图表(高级抽象),而不是示例中的数字(中级抽象)。
示例代码
p = figure(title="simple line example", plot_height=300, plot_width=600, y_range=(-5,5))
r = p.line(x, y, color="#2222aa", line_width=3)
def update(f, w=1, A=1, phi=0):
if f == "sin": func = np.sin
elif f == "cos": func = np.cos
elif f == "tan": func = np.tan
r.data_source.data['y'] = A * func(w * x + phi)
push_notebook()
我的代码:
p = Scatter(df_full_2d, x='X', y='Y', color='NPS Class',
title="Projection of NP Surver to 2D", legend="top_left",
legend_sort_field = 'color',
legend_sort_direction = 'ascending',
xlabel="Feature one",
ylabel="Feature two",
width=900,
height=600)
def update(f, perp=50):
if perp < 60:
p.title.text = 'Below'
else:
p.title.text = 'Above'
push_notebook()
标题正在更改,但如何在 Scatter 中访问更改数据?
提前致谢!
更新
我找到了更改数据的方法。
p.renderers
是一个列表,其中的 3 个元素(通过实验发现)确实代表了我的 scapper 上的 3 组不同的点。因此可以使用 p.renderers[1].data_source
引用一组的来源,但这是一种非常肮脏的方式。
我目前不建议将 push_notebook
与 bokeh.charts
结合使用。高级图表可能会在内部执行分组和聚合之类的操作,这实际上意味着必须重新计算整个图表。但是,如果您只是替换整个图表,则根本不需要 push_notebook
。
但是,最近的一些更改使 bokeh.plotting
中的事情变得更加简单。现在可以在浏览器中完成颜色映射,并且可以通过对列进行分组来自动创建图例。例如,从 Bokeh 0.12.3
开始,您可以这样做:
from bokeh.io import show
from bokeh.models import ColumnDataSource, CategoricalColorMapper
from bokeh.palettes import RdBu3
from bokeh.plotting import figure
source = ColumnDataSource(dict(
x=[1, 2, 3, 4, 5, 6],
y=[2, 1, 2, 1, 2, 1],
label=['hi', 'lo', 'hi', 'lo', 'hi', 'lo']
))
color_mapper = CategoricalColorMapper(factors=['hi', 'lo'],
palette=[RdBu3[2], RdBu3[0]])
p = figure(x_range=(0, 7), y_range=(0, 3), height=300, tools='save')
p.circle(x='x', y='y', radius=0.5, source=source,
color={'field': 'label', 'transform': color_mapper},
legend='label')
show(p)
所以我最强烈的建议是,如果您想利用 push_notebook
,请坚持使用 bokeh.plotting
。
我的想法是使用提供的交互器示例 in this sample repo 但是使用 Bokeh 图表(高级抽象),而不是示例中的数字(中级抽象)。
示例代码
p = figure(title="simple line example", plot_height=300, plot_width=600, y_range=(-5,5))
r = p.line(x, y, color="#2222aa", line_width=3)
def update(f, w=1, A=1, phi=0):
if f == "sin": func = np.sin
elif f == "cos": func = np.cos
elif f == "tan": func = np.tan
r.data_source.data['y'] = A * func(w * x + phi)
push_notebook()
我的代码:
p = Scatter(df_full_2d, x='X', y='Y', color='NPS Class',
title="Projection of NP Surver to 2D", legend="top_left",
legend_sort_field = 'color',
legend_sort_direction = 'ascending',
xlabel="Feature one",
ylabel="Feature two",
width=900,
height=600)
def update(f, perp=50):
if perp < 60:
p.title.text = 'Below'
else:
p.title.text = 'Above'
push_notebook()
标题正在更改,但如何在 Scatter 中访问更改数据?
提前致谢!
更新
我找到了更改数据的方法。
p.renderers
是一个列表,其中的 3 个元素(通过实验发现)确实代表了我的 scapper 上的 3 组不同的点。因此可以使用 p.renderers[1].data_source
引用一组的来源,但这是一种非常肮脏的方式。
我目前不建议将 push_notebook
与 bokeh.charts
结合使用。高级图表可能会在内部执行分组和聚合之类的操作,这实际上意味着必须重新计算整个图表。但是,如果您只是替换整个图表,则根本不需要 push_notebook
。
但是,最近的一些更改使 bokeh.plotting
中的事情变得更加简单。现在可以在浏览器中完成颜色映射,并且可以通过对列进行分组来自动创建图例。例如,从 Bokeh 0.12.3
开始,您可以这样做:
from bokeh.io import show
from bokeh.models import ColumnDataSource, CategoricalColorMapper
from bokeh.palettes import RdBu3
from bokeh.plotting import figure
source = ColumnDataSource(dict(
x=[1, 2, 3, 4, 5, 6],
y=[2, 1, 2, 1, 2, 1],
label=['hi', 'lo', 'hi', 'lo', 'hi', 'lo']
))
color_mapper = CategoricalColorMapper(factors=['hi', 'lo'],
palette=[RdBu3[2], RdBu3[0]])
p = figure(x_range=(0, 7), y_range=(0, 3), height=300, tools='save')
p.circle(x='x', y='y', radius=0.5, source=source,
color={'field': 'label', 'transform': color_mapper},
legend='label')
show(p)
所以我最强烈的建议是,如果您想利用 push_notebook
,请坚持使用 bokeh.plotting
。