我需要呈现一个动态散点图,从一个数据帧构建。用户应该能够在多个类别中选择要显示的类别(或显示所有类别)。我让博基更新了图例,显示了可能的类别,但没有标记本身。你知道吗
我在Jupyter笔记本和Bokeh服务器上都尝试过这个代码,它们的行为都是一样的。我也尝试过在不同的浏览器(Chrome,Firefox,甚至是优秀的olinternetexplorer)上运行代码,但没有成功。你知道吗
下面是我正在做的一个片段,其中df
是一个包含列Client
、Client Sector
、Volume
和Profit Margin
的Pandas数据帧,其思想是绘制体积和边距:
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))
正如预期的那样,这将生成一个散点图,其中标记根据客户的扇区着色,以及扇区及其颜色的图例。用于选择单个扇区的小部件也在那里。但是,当我选择单个扇区时,图例会改变(剩余扇区的颜色变为蓝色,这是原始图例中第一个扇区的颜色),但标记及其颜色保持不变(尽管悬停工具停止工作)。你知道吗
Bokeh
ColumnDataSource
对象非常重,与许多其他对象都有连接。基本上不支持批发替换。每个示例或文档片段始终且仅显示通过更新其.data
属性来更新现有CD,这是您应该使用的方式:最新版本支持直接从数据帧进行设置。否则,可以显式调用cd上的
data_from_df
静态方法。你知道吗相关问题 更多 >
编程相关推荐