gpt4 book ai didi

python - 如何使用下拉列表更新多个图形的 y 轴列?

转载 作者:太空宇宙 更新时间:2023-11-03 20:31:13 24 4
gpt4 key购买 nike

我有一个包含多列的 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)

最佳答案

您可以在 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关于动态更新布局以获得更多想法

关于python - 如何使用下拉列表更新多个图形的 y 轴列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57494997/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com