【发布时间】:2019-09-10 05:31:28
【问题描述】:
我需要展示一个从 DataFrame 构建的动态散点图。用户应该能够在多个类别中选择要显示的类别(或显示所有类别)。我让 Bokeh 更新了显示可能类别的图例,而不是标记本身。
我已经在 Jupyter 笔记本和 Bokeh 服务器上尝试过代码,它们的行为是相同的。我还尝试在不同的浏览器(Chrome、Firefox,甚至是优秀的 Internet Explorer)上运行代码,但没有骰子。
这是我正在做的事情的 sn-p,其中df 是一个 Pandas DataFrame,其中包含 Client、Client Sector、Volume 和 Profit Margin 列,其想法是绘制两个卷和边距:
SECTORS = list(df["Client Sector"].unique())
tools = ["box_select", "hover", "reset", "box_zoom", "wheel_zoom", "pan"]
TOOLTIPS = [
("Client", "@Client"),
("Activity", "@{Client Sector}"),
("Volume", "@Volume"),
("Margin", "@{Profit Margin}")
]
source = ColumnDataSource(data = df)
p = figure(plot_width=1600, plot_height=800, tools=tools, tooltips=TOOLTIPS)
r = p.circle('Volume', 'Profit Margin', source=source, size=10,
color=factor_cmap('Client Sector', 'Category10_7', SECTORS),
alpha=0.5, legend='Client Sector', hover_color='black')
def update():
sector_val = sector.value
if sector_val == "All":
selected = df
else:
selected = df[df["Client Sector"] == sector_val]
source = ColumnDataSource(data=selected)
r.data_source = source
sector = Select(title="Sector", value="All", options=["All"] + SECTORS)
sector.on_change('value', lambda attr, old, new: update())
update()
curdoc().add_root(row(ativ, p))
正如预期的那样,这会生成一个散点图,其中包含根据客户所在行业着色的标记,以及行业及其颜色的图例。用于选择单个扇区的小部件也在那里。但是,当我选择单个扇区时,图例会发生变化(剩余扇区的颜色变为蓝色,即原始图例中第一个扇区的颜色),但标记及其颜色保持不变(尽管悬停工具停止工作)。
【问题讨论】: