我正在对表格数据进行交互式可视化。我正在使用Bokeh(版本1.4.0)。问题是在输出html文件中什么都没有出现。我正在关注this示例(final result)。
这个想法是我想与之交互的熊猫数据框格式的信息。我想显示它并能够执行链接的示例中显示的所有内容。
代码是:
from os.path import join, dirname
import pandas as pd
import numpy as np
from bokeh.io import curdoc
from bokeh.plotting import figure, output_file, show
from bokeh.embed import components
from bokeh.models import ColumnDataSource, CustomJS, Slider, Div, Select, TextInput
from bokeh.models.tools import HoverTool
from bokeh.layouts import grid, column, layout
#data_path = 'DB1.csv'
#data_loaded = pd.read_csv(data_path)
#data_loaded = data_loaded.drop(columns=data_loaded.columns[0], axis=1)
syntetic_columns = ['id_pieza', 'xVar', 'yVar', 'zVar', 'N', 'P', 'vv', 'v']
syntetic_data = [['001', 1, 1, 1, 1, 10, 12, 15],
['004', 7.5, 7.1, 5.1, 1, 12, 12, 1.5],
['005', .1, 1, 1.54, 2, 9, 1.2, 1.5],
['006', .51, 1, 1.45, 1, 8.5, 12, 15],]
data_loaded = pd.DataFrame(data=syntetic_data, columns=syntetic_columns)
data_loaded['color'] = np.where(data_loaded['N'] == 1, 'orange', 'grey')
data_loaded['alpha'] = np.where(data_loaded['N'] == 1, '0.9', '0.25')
source = ColumnDataSource(data_loaded)
desc = Div(text=open(join(dirname(__file__), "description.html")).read(), sizing_mode="stretch_width")
axis_map = {
'Number of pieces': 'N',
'Cost of one unit': 'P',
'X maximum value': 'xVar',
'Y maximum value': 'yVar',
'Z maximum value': 'zVar',
}
x_axis = Select(title="X Axis", options=sorted(axis_map.keys()), value="Number of pieces")
y_axis = Select(title="Y Axis", options=sorted(axis_map.keys()), value="Cost of one unit")
xVal_maximun_slider = Slider(start=0,
end=max(data_loaded['xVar'].values),
value=max(data_loaded['xVar'].values),
step=0.1,
title='X maximum value')
TOOLTIPS = [
('Cost per unit', '@P'),
('Number of pieces', '@N'),
('id', '@id_pieza'),
('x value', '@xVar'),
('y value', '@yVar'),
('z value', '@zVar'),
('Volumen de viruta', '@vv')
]
p = figure(plot_height=600,
plot_width=700,
title="",
toolbar_location=None,
tooltips=TOOLTIPS,
sizing_mode="scale_both")
p.circle(x="x",
y="y",
source=source,
size=7,
color="color",
line_color=None,
fill_alpha="alpha")
def select_movies():
selected = data_loaded
return selected
def update():
df = select_movies()
x_name = axis_map[x_axis.value]
y_name = axis_map[y_axis.value]
p.xaxis.axis_label = x_axis.value
p.yaxis.axis_label = y_axis.value
source.data = dict(
x = df[x_name],
y = df[y_name],
color=df['color'],
alpha=df['alpha'],
)
controls = [xVal_maximun_slider]
for control in controls:
control.on_change('value', lambda attr, old, new: update())
inputs = column(*controls, width=320, height=1000)
inputs.sizing_mode = "fixed"
l = layout([
[desc],
[inputs, p],
], sizing_mode="scale_both")
update() # initial load of the data
curdoc().add_root(l)
我无法使用提供的代码重现该错误,但可能与更新功能的这一部分有关:
source.data = dict(
x = df[x_name],
y = df[y_name],
color=df['color'],
alpha=df['alpha'],
)
您的ColumnDataSource(此处是source
所指的名称)没有具有这些名称的列。