我正在使用 Bokeh v2.4.3。我正在使用 CDSView 和 IndexFilter 传递 pandas 数据框的列索引,但在途中不知何故迷路了。我的代码:
def tab_plots(df, cols1, cols2, cols3, title):
'''df - dataframe source, cols - list of column numbers from the df, title - chart titel '''
dates = df.index.to_numpy(dtype=np.datetime64)
source = ColumnDataSource(df)
view = CDSView(source=source, filters=[IndexFilter(cols1)])
view2 = CDSView(source=source, filters=[IndexFilter(cols2)])
view3 = CDSView(source=source, filters=[IndexFilter(cols3)])
#Slider dates
value2 = int(round(len(df.index)-1))
value1 = int(value2 -(value2/4))
##Chart size
hight = 300
width = 500
##Hovertool configuration
hover1 = HoverTool(tooltips=[("Date", "@date{%d/%m/%Y}"),
("Price", "@y{€ %.2f}")],
formatters={'@date': 'datetime',
'@y': 'printf'},
mode='vline')
hover2 = HoverTool(tooltips=[("Date", "@date{%d/%m/%Y}"),
("Price", "@y{€ %.2f}")],
formatters={'@date': 'datetime',
'@y': 'printf'},
mode='vline')
hover3 = HoverTool(tooltips=[("Date", "@date{%d/%m/%Y}"),
("Price", "@y{€ %.2f}")],
formatters={'@date': 'datetime',
'@y': 'printf'},
mode='vline')
fig1 = figure(plot_width=width,
plot_height=hight,
x_axis_type="datetime",
title = title,
toolbar_location="above",
tools=[hover1, BoxZoomTool(),
ResetTool(),
PanTool(),
SaveTool()],
x_range=(dates[value1], dates[value2]))
fig1.toolbar.logo = "grey"
fig1.line('date', cols1, source=source, view=view, line_color='green', hover_color="red")
fig1.yaxis.axis_label = '€'
tab1 = Panel(child=fig1, title="Base")
#####################
###Defining rangetool
select = figure(height=75,
width=width,
y_range=fig1.y_range,
x_axis_type="datetime",
y_axis_type=None,
tools="",
toolbar_location=None,
background_fill_color="#efefef")
range_tool = RangeTool(x_range=fig1.x_range)
range_tool.overlay.fill_color = "navy"
range_tool.overlay.fill_alpha = 0.2
select.line('date', 'y', source=source)
#select.line('date', 'y1', source=source)
#select.line('date', 'y2', source=source)
select.ygrid.grid_line_color = None
select.add_tools(range_tool)
select.toolbar.active_multi = range_tool
fig2 = figure(plot_width=width,
plot_height=hight,
x_axis_type="datetime",
title = title,
toolbar_location="above",
tools=[hover2, BoxZoomTool(),
ResetTool(),
PanTool(),
SaveTool()],
x_range=fig1.x_range)
fig2.toolbar.logo = "grey"
fig2.line('date', cols2, source=source, view=view2, line_color='red', hover_color="blue")
fig2.yaxis.axis_label = '€'
tab2 = Panel(child=fig2, title="Peak")
fig3 = figure(plot_width=width,
plot_height=hight,
x_axis_type="datetime",
title = title,
toolbar_location="above",
tools=[hover3, BoxZoomTool(),
ResetTool(),
PanTool(),
SaveTool()],
x_range=fig1.x_range)
fig3.toolbar.logo = "grey"
fig3.line('date', cols3, source=source, view=view3, line_color='blue', hover_color="red")
fig3.yaxis.axis_label = '€'
tab3 = Panel(child=fig3, title="PB Spread")
all_tabs = Tabs(tabs=[tab1, tab2, tab3])
return all_tabs, select
当我尝试运行函数时:
plot1, select = tab_plots(df=dfbase,
cols1=[2],
cols2=[3],
cols3=[5],
title='Title1')
##Graph 2
plot2, select2 = tab_plots(df=de_spot_m,
cols1=[1],
cols2=[2],
cols3=[4],
title='Title2')
##Graph 3
plot3, select3 = tab_plots(df=M_rolled_fut,
cols1=[1,2,3,4],
cols2=[5,6,7,8],
cols3=[9,10,11,12],
title='Title3')
grid = gridplot([[plot1, plot2, plot3],
[select, select2, select3]])
show(grid)
我收到以下错误:“RuntimeError:
期望 y 引用提供的数据源中的字段。
当“源”参数传递给字形方法时,值是序列 (如列表或数组)必须来自对源中数据列的引用。”
是的,我确实理解错误消息,但不知道如何更正代码...
非常感谢任何帮助!