Add an example using CategoricalColorMapper and Legend
See original GitHub issuee.g.
from bokeh.io import show
from bokeh.models import ColumnDataSource, CategoricalColorMapper
from bokeh.palettes import RdBu3
from bokeh.plotting import figure
source = ColumnDataSource(dict(
x=[1, 2, 3, 4, 5, 6],
y=[2, 1, 2, 1, 2, 1],
label=['hi', 'lo', 'hi', 'lo', 'hi', 'lo']
))
color_mapper = CategoricalColorMapper(factors=['hi', 'lo'], palette=[RdBu3[2], RdBu3[0]])
p = figure(x_range=(0, 7), y_range=(0, 3), height=300, tools='save')
p.circle(
x='x', y='y', radius=0.5, source=source,
color={'field': 'label', 'transform': color_mapper},
legend='label'
)
show(p)
Issue Analytics
- State:
- Created 7 years ago
- Reactions:6
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Add an example using CategoricalColorMapper and Legend
Add an example using CategoricalColorMapper and Legend ... e.g. from bokeh.io import show from bokeh.models import ColumnDataSource, ...
Read more >Handling categorical data — Bokeh 2.4.3 Documentation
For example, you can use nested sequences of strings to represent the individual ... Use the CategoricalColorMapper model to map bar colors in...
Read more >How do I create a legend for a heatmap in Bokeh 12.4.1
I figured out a way through using CategoricalColorMapper and then not creating an explicit legend object. There may be a way to create...
Read more >bokeh: how to add legend to patches glyph with geojsondatasource ...
bokeh: how to add legend to patches glyph with geojsondatasource and categoricalcolormapper? from bokeh.models import GeoJSONDataSource, CategoricalColorMapper ...
Read more >Scatter Plots with Bokeh
We will be adding now the names of the Athlets in the graph. We need first to import some functions and transform decathlon...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I think this a very nice example, actually. I was looking for CategorialColorMapper and found its usage only here. I think the documentation would benefit from such example. The referenced page (legends) shows only a variant of this and it is not as elegant as the one from @birdsarah
I also agree with @mficek. The example here uses CategorialColorMapper to bind a categorical variable, instead of explicitly calling out the color in the ColumnDataSource in the user_guide example.
I would also add, that in explorative data analysis, we often don’t know the number of unique levels in a categorical variable “before” we want to plot it.
So an even more powerful example would include 1) Counting the number of unique levels. 2) Picking an appropriate categorical pallet with matching number colors.
If the number of levels is, say, greater then the unique colors in the chosen palette, then recycle the colors starting at [0]