question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add an example using CategoricalColorMapper and Legend

See original GitHub issue

e.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)

bokeh_plot

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:6
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

6reactions
mficekcommented, Apr 20, 2017

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

3reactions
rsgoodwincommented, Nov 11, 2017

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]

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found