Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import Bokeh palettes

Tags:

python

bokeh

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
like image 640
DougKruger Avatar asked May 03 '17 10:05

DougKruger


People also ask

What is a palette in bokeh?

A palette is a simple plain Python list of (hex) RGB color strings. For example, the blues8 palette has the colors : ('#084594', '#2171b5', '#4292c6', '#6baed6', '#9ecae1', '#c6dbef', '#deebf7', '#f7fbff'). There are 5 types of built-in color palettes in Bokeh : Matplotlib Palettes. D3 Palettes.

What is bokeh in Python?

Bokeh is a Python interactive data visualization. It renders its plots using HTML and JavaScript. It targets modern web browsers for presentation providing elegant, concise construction of novel graphics with high-performance interactivity. Bokeh provides us with multiple color palettes in the bokeh.palettes module.

Is it possible to import category20 palettes in bokeh?

10 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. Share Follow answered May 3 '17 at 11:42 e.arbitrioe.arbitrio 44044 silver badges1414 bronze badges 5 1

How many types of D3 palettes are available in bokeh?

There are 4 types of D3 color palettes available : Example : We will be demonstrating the D3 palettes by plotting multiple vertical bars using the vbar () function. Bokeh provides us with ColorBrewer palettes. There are 35 types of ColorBrewer palettes available :


3 Answers

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.

like image 162
bjk Avatar answered Sep 30 '22 05:09

bjk


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.

like image 43
e.arbitrio Avatar answered Sep 30 '22 05:09

e.arbitrio


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]
like image 31
Vivek Sarma Avatar answered Sep 30 '22 07:09

Vivek Sarma