使用 click_policy="hide" 从散景图中删除所有数据

All data being remove from bokeh plot using click_policy="hide"

我正在尝试使用散景绘制交互式绘图以在二维图表中可视化 t-SNE 数据。它应该显示 9 种衣服类别。请参阅下面的代码和变量。

df:

             x         y       labels   colors                             imgs
0     0.017387 -0.270469  T-shirt/top  #1f77b4     ./fashion_mnist_images/0.png
1    -0.306095 -0.099984      Trouser  #ff7f0e     ./fashion_mnist_images/1.png
2     0.066467  0.020477     Pullover  #2ca02c     ./fashion_mnist_images/2.png
3     0.170224  0.000206     Pullover  #2ca02c     ./fashion_mnist_images/3.png
4    -0.029488 -0.157343        Dress  #d62728     ./fashion_mnist_images/4.png
       ...       ...          ...      ...                              ...
9995 -0.024929 -0.099063  T-shirt/top  #1f77b4  ./fashion_mnist_images/9995.png
9996  0.044125 -0.013741        Shirt  #e377c2  ./fashion_mnist_images/9996.png
9997  0.101816  0.103228          Bag  #bcbd22  ./fashion_mnist_images/9997.png
9998  0.260997  0.035555          Bag  #bcbd22  ./fashion_mnist_images/9998.png
9999  0.106888 -0.178166      Trouser  #ff7f0e  ./fashion_mnist_images/9999.png
[10000 rows x 5 columns]

代码:

def plot_tsne_data(title):
    source = ColumnDataSource(
        data = dict(
                x = df['x'].values,
                y = df['y'].values,
                img = df['imgs'].values,
                label = df['labels'].values,
                color = df['colors'].values
            )
        )
    hover = HoverTool(
            tooltips="""
            <div>
                <img src="@img" height="28" alt="@img" width="28"></img>
            </div>
            """
        )
    tools = [hover, "pan", "wheel_zoom", "box_zoom", "reset"]
    plot = figure(plot_width=784, plot_height=600, title=title, tools=tools, toolbar_location="below")
    plot.circle("x", "y", size=3, color='color', legend_group='label', source=source)
    plot.add_layout(plot.legend[0], 'right')
    plot.legend.click_policy="hide"
    return plot
        
fig = plot_tsne_data("Fashion MNIST - t-SNE")
show(fig)

输出图:

情节似乎一切都很好,除非我尝试点击任何图例。当用户单击图例中的一项(如“Trouser”)时,它应该只删除 Trouser,但它会删除所有数据。如果有人能提供帮助,我将不胜感激。

示例图片:

不幸的是,图例 hiding/muting 与自动分组的图例不兼容。您需要为每个组分别调用 circle(改为 legend_label),以便它们可以单独隐藏。