散景:push_notebook()之前的散点图的chage数据

时间:2016-11-02 09:33:57

标签: bokeh

我的想法是使用Interactor示例提供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来引用一个组的来源,但这是一种相当肮脏的方式。

1 个答案:

答案 0 :(得分:1)

我目前不建议将push_notebookbokeh.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)

enter image description here

因此,如果您想利用bokeh.plotting,我最强烈的建议就是坚持使用push_notebook