Skip to content
Related Articles

Related Articles

Improve Article

Python Bokeh – Colors Class

  • Last Updated : 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.

In this article, we will learn about colors in Bokeh. There are 5 different classes for colors in Bokeh :

  1. bokeh.colors.Color
  2. bokeh.colors.HSL
  3. bokeh.colors.RGB
  4. bokeh.colors.groups
  5. bokeh.colors.names

bokeh.colors.Color

This is the base class representing the color objects. The methods in this class are :

  • clamp()
  • copy()
  • darken()
  • from_hsl()
  • from_rgb()
  • lighten()
  • to_css()
  • to_hsl()
  • to_rgb()

bokeh.colors.HSL

This provides a class to represent colors with HSL format, i.e. Hue, Value and Lightness. 

Example : We will plot a glyph and use the HSL format to color it.



Python3




# importing the modules 
from bokeh.plotting import figure, output_file, show 
from bokeh.colors import HSL
  
# file to save the model 
output_file("gfg.html"
      
# instantiating the figure object 
graph = figure(title = "Bokeh Color"
      
# the point to be plotted 
x = 0
y = 0
  
# HSL color
hue = 0
saturation = 0.47
lightness = 0.58
color = HSL(h = hue,
            s = saturation,
            l = lightness)
  
# plotting the graph 
graph.dot(x, y, size = 1000,
          color = color.to_rgb()) 
      
# displaying the model 
show(graph)

 

 

Output :

 

 

bokeh.colors.RGB

 



This represents colors by specifying their Red, Green, and Blue channels.

 

Example : We will plot a glyph and use the RGB format to color it in light green.

 

Python3




# importing the modules 
from bokeh.plotting import figure, output_file, show 
from bokeh.colors import RGB
  
# file to save the model 
output_file("gfg.html"
      
# instantiating the figure object 
graph = figure(title = "Bokeh Color"
      
# the point to be plotted 
x = 0
y = 0
  
# RGB color
red = 144
green = 238
blue = 144
color = RGB(r = red,
            g = green,
            b = blue)
  
# plotting the graph 
graph.dot(x, y, size = 1000,
          color = color) 
      
# displaying the model 
show(graph)

 

 

Output : 

 



 

bokeh.colors.groups

 

This represents CSS named colors into useful groups according to the general hue. Each color has its own class with multiple colors of different hues. The list of colors present in this class are :

 

  1. black : gainsboro, lightgray, silver, darkgray, gray, dimgray, lightslategray, slategray, darkslategray, black
  2. blue : lightsteelblue, powderblue, lightblue, skyblue, lightskyblue, deepskyblue, dodgerblue, cornflowerblue, steelblue, royalblue, blue, mediumblue, darkblue, navy, midnightblue
  3. brown : cornsilk, blanchedalmond, bisque, navajowhite, wheat, burlywood, tan, rosybrown, sandybrown, goldenrod, darkgoldenrod, peru, chocolate, saddlebrown, sienna, brown, maroon
  4. cyan : mediumaquamarine, aqua, cyan, lightcyan, paleturquoise, aquamarine, turquoise, mediumturquoise, darkturquoise, lightseagreen, cadetblue, darkcyan, teal
  5. green : darkolivegreen, olive, olivedrab, yellowgreen, limegreen, lime, lawngreen, chartreuse, greenyellow, springgreen, mediumspringgreen, lightgreen, palegreen, darkseagreen, mediumseagreen, seagreen, forestgreen, green, darkgreen
  6. orange : orangered, tomato, coral, darkorange, orange
  7. pink : pink, lightpink, hotpink, deeppink, palevioletred, mediumvioletred
  8. purple : lavender, thistle, plum, violet, orchid, fuchsia, magenta, mediumorchid, mediumpurple, blueviolet, darkviolet, darkorchid, darkmagenta, purple, indigo, darkslateblue, slateblue, mediumslateblue
  9. red : lightsalmon, salmon, darksalmon, lightcoral, indianred, crimson, firebrick, darkred, red
  10. white : white, snow, honeydew, mintcream, azure, aliceblue, ghostwhite, whitesmoke, seashell, beige, oldlace, floralwhite, ivory, antiquewhite, linen, lavenderblush, mistyrose
  11. yellow : yellow, lightyellow, lemonchiffon, lightgoldenrodyellow, papayawhip, moccasin, peachpuff, palegoldenrod, khaki, darkkhaki, gold

 

Example : We will plot multiple glyphs and use the color groups to color them.

 

Python3




# importing the modules 
from bokeh.plotting import figure, output_file, show 
from bokeh.colors.groups import purple, yellow, blue
  
# file to save the model 
output_file("gfg.html"
      
# instantiating the figure object 
graph = figure(title = "Bokeh Color"
      
# the point to be plotted 
x = [-2, 0, 2]
y = 0
  
# color groups
color = [purple()._colors[purple()._colors.index("Fuchsia")],
         yellow()._colors[yellow()._colors.index("Khaki")],
         blue()._colors[blue()._colors.index("RoyalBlue")]]
  
# plotting the graph 
graph.dot(x, y, size = 1000,
          color = color) 
      
# displaying the model 
show(graph)

Output : 



bokeh.colors.named

This class provides us with all the 147 CSS named colors. 

Example : We will plot multiple glyphs and use the color names to color them.

Python3




# importing the modules 
from bokeh.plotting import figure, output_file, show 
from bokeh.colors import named
  
# file to save the model 
output_file("gfg.html"
      
# instantiating the figure object 
graph = figure(title = "Bokeh Color"
      
# the point to be plotted 
x = [-3, 0, 3]
y = 0
  
# color names
color = ["bisque",
         "greenyellow",
         "magenta"]
  
# plotting the graph 
graph.dot(x, y, size = 1000,
          color = color) 
      
# displaying the model 
show(graph)

Output : 

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :