如何使用下拉菜单更新多个图形的y轴列?

时间:2019-08-14 12:45:41

标签: python bokeh

我有一个多列的pandas数据框。我有多个散点图,其中x轴与数据框不同,而y轴在所有图中均相同。我希望能够基于下拉菜单中的选定值更改y轴。我是使用bokeh的新手,我无法弄清楚如何为这种情况构建回调函数。任何帮助深表感谢!

    def callback(attr, old, new):
        y_axis = select.value
        source.data = {'y' : y_axis}
        p1.circle.y = y_axis



    columns = {'TC2B':'Temperature 2B','TC1B':'Temperature 1B', 'PCV-2006 Pos':'2006 valve position', 
       'TCV-2008 DownS Ctrl PV':'Down Stream'}

    select = Select(title='Y axis:', value='TCV-2008 Pos', options=list(columns.keys()))

    y_select = select.value
    color_select = 'TC2B'#'PCV-2006 Pos'
    #source = ColumnDataSource(data={'y':y_select})
    source = ColumnDataSource(data)



   p1 = figure(x_axis_label='Temperature 1B', y_axis_label=columns[y_select])

   p1.circle(x='TC1B', 
        y=y_select,
        source=source)

   p2 = figure(x_axis_label='2006 valve position', y_axis_label=columns[y_select])
   p2.circle(x='PCV-2006 Pos', 
        y=y_select,
        source=source)

   p3 = figure(x_axis_label='Down Stream', y_axis_label=columns[y_select])
   p3.circle(x='TCV-2008 DownS Ctrl PV', 
        y=y_select,
        source=source)

   p1.y_range = p2.y_range = p3.y_range

   select.on_change('value', callback)

   layout = column(select, row(column(p1, p2, p3)))

   curdoc().add_root(layout)

1 个答案:

答案 0 :(得分:0)

您可以在onchange回调中从头开始重新创建图并更新布局内容。看看这个:

from bokeh.models import ColumnDataSource, Select
from bokeh.plotting import figure
from bokeh.layouts import column, row
from bokeh.io import curdoc
import numpy as np

N = 3000
data = {
    'X': np.random.random(size=N) * 100,
    'Y': np.random.random(size=N) * 100,
    'Z': np.random.random(size=N) * 100,
    'A': np.random.random(size=N) * 100,
    'B': np.random.random(size=N) * 100,
    'C': np.random.random(size=N) * 100,
}

select = Select(title='Y axis:', value='Y', options=list(data.keys()))

source = ColumnDataSource(data=data)

p1 = figure(
    x_axis_label='X',
    y_axis_label='Y'
)

c1 = p1.circle(
    x='X',
    y='Y',
    source=source
)

p2 = figure(
    x_axis_label='A',
    y_axis_label='Y'
)
c2 = p2.circle(
    x='A',
    y='Y',
    source=source
)

p1.y_range = p2.y_range

r = row(children=[p1, p2])
layout = column(select, r)

def onchange_value(attr, old, new):
    p1 = p2 = None
    p1 = figure(
        x_axis_label='X',
        y_axis_label=new
    )

    p1.circle(
        x='X',
        y=new,
        source=source
    )

    p2 = figure(
        x_axis_label='A',
        y_axis_label=new
    )
    p2.circle(
        x='A',
        y=new,
        source=source
    )
    r.children = [p1, p2]

select.on_change('value', onchange_value)

curdoc().add_root(layout)

注意:您还可以阅读this other question,有关动态更新布局以获取更多想法