How to use Color Palettes in Python-Bokeh?

28 Jul, 2020
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. Let us see how to use these color palettes 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 PalettesD3 PalettesBrewer PalettesColor-Deficient Usability PaletteLarge Palettes
Matplotlib Palettes
D3 Palettes
Brewer Palettes
Color-Deficient Usability Palette
Large Palettes
Bokeh provides us with Matplotlib color palettes. There are 5 types of Matplotlib color palettes :
Magma
Inferno
Plasma
Viridis
Cividis
Each type of color palette has 10 different versions of the palette with varying number of colors, which are 3, 4, 5, 6, 7, 8, 9, 10, 11 and 256.
Example : We will be demonstrating the Matplotlib palettes by plotting multiple vertical bars using the vbar() function.
# importing the modules from bokeh.plotting import figure, output_file, show from bokeh.palettes import Magma, Inferno, Plasma, Viridis, Cividis  # file to save the model output_file("gfg.html")          # instantiating the figure object graph = figure(title = "Bokeh Palettes")   # demonstrating the Magma palettegraph.vbar(x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],            top = [9] * 11,           bottom = [8] * 11,           width = 1,           color = Magma[11])  # demonstrating the Inferno palettegraph.vbar(x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],            top = [7] * 11,           bottom = [6] * 11,           width = 1,           color = Inferno[11])  # demonstrating the Plasma palettegraph.vbar(x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],            top = [5] * 11,           bottom = [4] * 11,           width = 1,           color = Plasma[11])  # demonstrating the Viridis palettegraph.vbar(x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],            top = [3] * 11,           bottom = [2] * 11,           width = 1,           color = Viridis[11])  # demonstrating the Cividis palettegraph.vbar(x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],            top = [1] * 11,           width = 1,           color = Cividis[11])     # displaying the model show(graph)
Output :
Bokeh provides us with D3 categorical color palettes. There are 4 types of D3 color palettes available :
Category10
Category20
Category20b
Category20c
Example : We will be demonstrating the D3 palettes by plotting multiple vertical bars using the vbar() function.
# importing the modules from bokeh.plotting import figure, output_file, show from bokeh.palettes import Category10, Category20, Category20b, Category20c  # file to save the model output_file("gfg.html")          # instantiating the figure object graph = figure(title = "Bokeh Palettes")   # demonstrating the Category10 palettegraph.vbar(x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],            top = [9] * 10,           bottom = [8] * 10,           width = 1,           color = Category10[10])  # demonstrating the Category20 palettegraph.vbar(x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],            top = [7] * 10,           bottom = [6] * 10,           width = 1,           color = Category20[10])  # demonstrating the Category20b palettegraph.vbar(x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],            top = [5] * 10,           bottom = [4] * 10,           width = 1,           color = Category20b[10])  # demonstrating the Category20c palettegraph.vbar(x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],            top = [3] * 10,           bottom = [2] * 10,           width = 1,           color = Category20c[10])  # displaying the model show(graph)
Output :
Bokeh provides us with ColorBrewer palettes. There are 35 types of ColorBrewer palettes available :
Accent
Blues
BrBG
BuGn
BuPu
Dark2
GnBu
Greens
Greys
OrRd
Oranges
PRGn
Paired
Pastel1
Pastel2
PiYG
PuBu
PuBuGn
PuOr
PuRd
Purples
RdBu
RdGy
RdPu
RdYlBu
RdYlGn
Reds
Set1
Set2
Set3
Spectral
YlGn
YlGnBu
YlOrBr
YlOrRd
Example : We will be demonstrating the ColorBrewer palettes by plotting multiple vertical bars using the vbar() function.
# importing the modules from bokeh.plotting import figure, output_file, show from bokeh.palettes import BrBG, PiYG, RdGy, RdYlGn, YlGnBu  # file to save the model output_file("gfg.html")          # instantiating the figure object graph = figure(title = "Bokeh Palettes")   # demonstrating the BrBG palettegraph.vbar(x = [1, 2, 3, 4, 5, 6, 7, 8, 9],            top = [9] * 9,           bottom = [8] * 9,           width = 1,           color = BrBG[9])  # demonstrating the PiYG palettegraph.vbar(x = [1, 2, 3, 4, 5, 6, 7, 8, 9],            top = [7] * 9,           bottom = [6] * 9,           width = 1,           color = PiYG[9])  # demonstrating the RdGy palettegraph.vbar(x = [1, 2, 3, 4, 5, 6, 7, 8, 9],            top = [5] * 9,           bottom = [4] * 9,           width = 1,           color = RdGy[9])  # demonstrating the RdYlGn palettegraph.vbar(x = [1, 2, 3, 4, 5, 6, 7, 8, 9],            top = [3] * 9,           bottom = [2] * 9,           width = 1,           color = RdYlGn[9])  # demonstrating the YlGnBu palettegraph.vbar(x = [1, 2, 3, 4, 5, 6, 7, 8, 9],            top = [1] * 9,           width = 1,           color = YlGnBu[9])  # displaying the model show(graph) 
Output :
Bokeh provides us with a palette that is useful for people with color deficiency or color blindness.
Example : We will be demonstrating the usability palette by plotting multiple vertical bars using the vbar() function.
# importing the modules from bokeh.plotting import figure, output_file, show from bokeh.palettes import Colorblind  # file to save the model output_file("gfg.html")          # instantiating the figure object graph = figure(title = "Bokeh Palettes")   # demonstrating the Colorblind palettegraph.vbar(x = [1, 2, 3, 4, 5, 6, 7, 8],            top = [1] * 8,           width = 1,           color = Colorblind[8])  # displaying the model show(graph)
Output :
The color palettes discussed above might be small for some applications. Bokeh provides us with large palettes that have 256 colors each. There are 7 large palettes :
Greys256
Inferno256
Magma256
Plasma256
Viridis256
Cividis256
Turbo256
Example : We will be demonstrating the large palettes by plotting multiple vertical bars using the vbar() function.
# importing the modules from bokeh.plotting import figure, output_file, show from bokeh.palettes import Greys256, Inferno256, Magma256, Plasma256from bokeh.palettes import Viridis256, Cividis256, Turbo256  # file to save the model output_file("gfg.html")          # instantiating the figure object graph = figure(title = "Bokeh Palettes")   # demonstrating the Greys256 palettegraph.vbar(x = [i for i in range(256)],            top = [20] * 256,           bottom = [18] * 256,           width = 1,           color = Greys256)  # demonstrating the Inferno256 palettegraph.vbar(x = [i for i in range(256)],            top = [17] * 256,           bottom = [15] * 256,           width = 1,           color = Inferno256)  # demonstrating the Magma256 palettegraph.vbar(x = [i for i in range(256)],            top = [14] * 256,           bottom = [12] * 256,           width = 1,           color = Magma256)  # demonstrating the Plasma256 palettegraph.vbar(x = [i for i in range(256)],            top = [11] * 256,           bottom = [9] * 256,           width = 1,           color = Plasma256)  # demonstrating the Viridis256 palettegraph.vbar(x = [i for i in range(256)],            top = [8] * 256,           bottom = [6] * 256,           width = 1,           color = Viridis256)  # demonstrating the Cividis256 palettegraph.vbar(x = [i for i in range(256)],            top = [5] * 256,           bottom = [3] * 256,           width = 1,           color = Cividis256)  # demonstrating the Turbo256 palettegraph.vbar(x = [i for i in range(256)],            top = [2] * 256,           bottom = [0] * 256,           width = 1,           color = Turbo256)  # displaying the model show(graph) 
Output :
Python-Bokeh
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Enumerate() in Python
Different ways to create Pandas Dataframe
How to Install PIP on Windows ?
Python String | replace()
*args and **kwargs in Python
Python OOPs Concepts
Python Classes and Objects
Introduction To PYTHON
Create a Pandas DataFrame from Lists