10

How to use the D3 Palettes in Bokeh? I tried importing this way but I get an unresolved reference error message

from bokeh.palettes import Category20

Bokeh version:

print bokeh.__version__

0.11.1
DougKruger
  • 4,424
  • 14
  • 41
  • 62

4 Answers4

12

In bokeh 0.11.1 the Category20 palette does not exist

It's implemented in the 0.12.4 (the latest one), and works perfectly

from bokeh.palettes import Category20

Let's try to update it if you can.

e.arbitrio
  • 556
  • 4
  • 14
5
Category20:
{3: ['#1f77b4', '#aec7e8', '#ff7f0e'],
 4: ['#1f77b4', '#aec7e8', '#ff7f0e', '#ffbb78'],
 5: ['#1f77b4', '#aec7e8', '#ff7f0e', '#ffbb78', '#2ca02c'],
 6: ['#1f77b4', '#aec7e8', '#ff7f0e', '#ffbb78', '#2ca02c', '#98df8a'],
 7: ['#1f77b4',.... ]
.
.
20: []

So best way to use is:

Category20[20][0]
Vivek Sarma
  • 51
  • 1
  • 3
1

All answers so far refer to the pre-built palettes available as module attributes, but it may help others to note that there are also functions that can be used to generate palettes in Bokeh.

First, note that each of the module attribute palettes is a dictionary of tuples, each indexed by length n and containing a list of hex color codes of that length. e.g., I'll use Colorblind palette, because it's smaller:

>>> from bokeh.palettes import Colorblind
>>> Colorblind
{3: ('#0072B2', '#E69F00', '#F0E442'), 4: ('#0072B2', '#E69F00', '#F0E442', '#009E73'), 5: ('#0072B2', '#E69F00', '#F0E442', '#009E73', '#56B4E9'), 6: ('#0072B2', '#E69F00', '#F0E442', '#009E73', '#56B4E9', '#D55E00'), 7: ('#0072B2', '#E69F00', '#F0E442', '#009E73', '#56B4E9', '#D55E00', '#CC79A7'), 8: ('#0072B2', '#E69F00', '#F0E442', '#009E73', '#56B4E9', '#D55E00', '#CC79A7', '#000000')}`

Then the Colorblind palette of length-3 is accessed as:

>>> Colorblind[3]
('#0072B2', '#E69F00', '#F0E442')

There are also large 256 color palettes (e.g., Cividis256), which are also just tuples of 256 hex color codes.

But in addition to accessing palettes as attributes, the bokeh.palettes module also offers functions that can generate lists of colors of arbitrary size from the special larger palettes. Several of these large palettes and functions are described in the docs, e.g., to generate a palette of length 6 from the Cividis256 palette, the built-in cividis function can be used:

>>> from bokeh.palettes import cividis
>>> cividis(6)
('#00204C', '#31446B', '#666870', '#958F78', '#CAB969', '#FFE945')

Note also that some larger palettes available are not in the current (2.2.3) version of the docs (e.g. Reds256), but they can also be used with the linear_palette function to generate the same sort of thing, e.g., a palette of 20 reds:

>>> from bokeh.palettes import Reds256, linear_palette
>>> linear_palette(Reds256, 20)
('#67000d', '#800610', '#9a0c14', '#af1117', '#be151a', '#cf1c1f', '#dd2a25', '#ec382b', '#f34c37', '#f85f43', '#fb7252', '#fc8262', '#fc9474', '#fca588', '#fcb69b', '#fdc6b0', '#fdd5c4', '#fee3d7', '#ffece3', '#fff5f0')

The diverging_palette function is also worth checking out.

bjk
  • 100
  • 7
0

For those searching for information on bokeh palettes, the docs now have an excellent expanded list of available palettes.

Bokeh 2.2.3 includes D3 palettes 10, 20, 20b, 20c

For example, you can specify the D3 palette category and then specify as a string 'Category20b' group as the first index and then specify as an integer the number of colors in the group as the second index.

>> d3['Category20b'][4]
('#393b79', '#5254a3', '#6b6ecf', '#9c9ede')

If you can't find the palette you like, create your own using a python list of (hex) RGB color strings.

('#084594', '#2171b5', '#4292c6', '#6baed6', '#9ecae1', '#c6dbef', '#deebf7', '#f7fbff')