问题描述
我有一个pandas数据框,我想使用Bokeh服务器将其列显示为图中的线条.另外,我想有一个滑块来使其中一条线相对另一条线移动.
I have got a pandas dataframe whose columns I want to show as lines in a plot using a Bokeh server. Additionally, I would like to have a slider for shifting one of the lines against the other.
我的问题是滑块值更改时的更新功能.我已经尝试过bokeh的滑块示例中的代码,但是它不起作用.
My problem is the update functionality when the slider value changes. I have tried the code from the sliders-example of bokeh, but it does not work.
这是一个例子
import pandas as pd
from bokeh.io import vform
from bokeh.plotting import Figure, output_file, show
from bokeh.models import CustomJS, ColumnDataSource, Slider
df = pd.DataFrame([[1,2,3],[3,4,5]])
df = df.transpose()
myindex = list(df.index.values)
mysource = ColumnDataSource(df)
plot = Figure(plot_width=400, plot_height=400)
for i in range(len(mysource.column_names) - 1):
name = mysource.column_names[i]
plot.line(x = myindex, y = str(name), source = mysource)
offset = Slider(title="offset", value=0.0, start=-1.0, end=1.0, step=1)
def update_data(attrname, old, new):
# Get the current slider values
a = offset.value
temp = df[1].shift(a)
#to finish#
offset.on_change('value', update_data)
layout = vform(offset, plot)
show(layout)
在update_data
函数内部,我必须更新mysource
,但是我不知道该怎么做.有人能指出我正确的方向吗?
Inside the update_data
-function I have to update mysource
, but I cannot figure out how to do that. Can anybody point me in the right direction?
推荐答案
尝试一下...将a=offset.value
更改为a=cb_obj.get('value')
Give this a try... change a=offset.value
to a=cb_obj.get('value')
然后在尝试执行的所有操作之后将source.trigger('change')
放在该update_data
函数而不是offset.on_change('value', update_data)
中.
Then put source.trigger('change')
after you do whatever it is you are trying to do in that update_data
function instead of offset.on_change('value', update_data)
.
还要更改offset = Slider(title="offset", value=0.0, start=-1.0, end=1.0, step=1, callback=CustomJS.from_py_func(offset))
请注意,我使用的这种格式已安装flexx
. https://github.com/zoofio/flexx 如果您使用的是Python 3.5,则必须下载压缩文件,解压缩并键入python setup.py install
,因为尚未发布该版本的文件...
Note this format I'm using works with flexx
installed. https://github.com/zoofio/flexx if you have Python 3.5 you'll have to download the zip file, extract, and type python setup.py install
as it isn't posted yet compiled for this version...
这篇关于散景: pandas 数据框的图表不会在触发时更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!