Search code examples
python-3.xplotbokehlegend

Hiding Bokeh glyphs based on categorical data with color_mapper


Currently i found a way of plotting my categorical data by using the color_mapper and the legend_field variable. My data look the following way:

df_plot
              x          y    category
0     91.587097  -3.761215        cat1
1    169.068802 -11.131036        cat2
2     64.201126   3.333073        cat1
3     78.828232   6.910666        cat3
4     51.033684  -6.213269        cat1
..          ...        ...         ...
160  -24.078451    5.34703        cat1
161  104.982063  -1.674969        cat2
162   55.020164  -1.134725        cat3
163  233.567902    2.50064        cat2
164   83.931938    7.92193        cat1

I want to hide the glyphs based on the categorical legend like in the example from bokehs website:

 source = ColumnDataSource(data=df)
 category = ['cat1', 'cat2', 'cat3']
 color_mapper = factor_cmap('category', palette=Category10_3, factors=category)
        
 plot = figure(x_range=(-x_range, x_range),
               y_range=(y_range[0], y_range[1]),
               title=title,
               width=500,
               height=800,
               tools=TOOLS
               )
      
 plot.circle(x='x', 
             y='y',
             size=8, 
             source=source, 
             color=color_mapper,
             legend_field='category'
             ) 

plot.legend.click_policy='hide'

I get the following outcome:

Bokeh Plot

Right now i can only hide all categories at once. Is there a way to do this with the color_mapper or do I have to split the data beforehand and loop over each category?


Solution

  • you could filter data with CDSView, BooleanFilter to do that. here is little example. link with on_change or js on change.