📜  如何在 Python-Bokeh 中使用调色板?

📅  最后修改于: 2022-05-13 01:55:42.210000             🧑  作者: Mango

如何在 Python-Bokeh 中使用调色板?

Bokeh 是一个Python交互式数据可视化。它使用 HTML 和 JavaScript 渲染其绘图。它针对现代 Web 浏览器进行演示,提供具有高性能交互性的新颖图形的优雅、简洁构造。 Bokeh 在bokeh.palettes模块中为我们提供了多种调色板。让我们看看如何在 Bokeh 中使用这些调色板。

调色板是(十六进制)RGB 颜色字符串的简单纯Python列表。例如, blues8调色板的颜色为: ('#084594', '#2171b5', '#4292c6', '#6baed6', '#9ecae1', '#c6dbef', '#deebf7', '#f7fbff') .

Bokeh 中有 5 种类型的内置调色板:

  1. Matplotlib 调色板
  2. D3 调色板
  3. 布鲁尔调色板
  4. 色彩不足的可用性调色板
  5. 大调色板

Matplotlib 调色板

Bokeh 为我们提供了 Matplotlib 调色板。 Matplotlib 调色板有 5 种类型:

  • 岩浆
  • 地狱火
  • 等离子体
  • 维里迪斯
  • 西维迪斯

每种类型的调色板都有 10 个不同版本的调色板,颜色数量不同,分别是 3、4、5、6、7、8、9、10、11 和 256。

示例:我们将通过使用 vbar()函数绘制多个垂直条来演示 Matplotlib 调色板。

# 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 palette
graph.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 palette
graph.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 palette
graph.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 palette
graph.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 palette
graph.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)

输出 :

D3 调色板

Bokeh 为我们提供了 D3 分类调色板。有 4 种类型的 D3 调色板可用:

  • 类别10
  • 类别20
  • 类别20b
  • 类别20c

示例:我们将通过使用 vbar()函数绘制多个垂直条来演示 D3 调色板。

# 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 palette
graph.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 palette
graph.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 palette
graph.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 palette
graph.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)

输出 :

布鲁尔调色板

Bokeh 为我们提供了 ColorBrewer 调色板。有 35 种类型的 ColorBrewer 调色板可用:

  • 口音
  • 蓝调
  • 溴化硼
  • BUGn
  • 布普
  • 黑暗2
  • 氮化硼
  • 青菜
  • 格雷斯
  • 或Rd
  • 橘子
  • PRGn
  • 配对
  • 粉彩1
  • 粉彩2
  • 皮尤格
  • 普布
  • 普布金
  • 聚氨酯
  • 泥浆
  • 紫色
  • RdBu
  • 陀螺仪
  • RdPu
  • RdYlBu
  • RdYlGn
  • 红军
  • 设置1
  • 第 2 组
  • 第 3 组
  • 光谱
  • YlGn
  • 溴化铬
  • YLORRD

示例:我们将通过使用 vbar()函数绘制多个垂直条来演示 ColorBrewer 调色板。

# 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 palette
graph.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 palette
graph.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 palette
graph.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 palette
graph.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 palette
graph.vbar(x = [1, 2, 3, 4, 5, 6, 7, 8, 9], 
           top = [1] * 9,
           width = 1,
           color = YlGnBu[9])
  
# displaying the model 
show(graph) 

输出 :

可用性调色板

Bokeh 为我们提供了一个对色盲或色盲人士有用的调色板。

示例:我们将通过使用 vbar()函数绘制多个垂直条来演示可用性调色板。

# 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 palette
graph.vbar(x = [1, 2, 3, 4, 5, 6, 7, 8], 
           top = [1] * 8,
           width = 1,
           color = Colorblind[8])
  
# displaying the model 
show(graph)

输出 :

大调色板

上面讨论的调色板对于某些应用程序可能很小。 Bokeh 为我们提供了大调色板,每个调色板有 256 种颜色。有 7 个大调色板:

  • 灰色256
  • 地狱火256
  • 岩浆256
  • 等离子256
  • Viridis256
  • Cividis256
  • 涡轮256

示例:我们将通过使用 vbar()函数绘制多个垂直条来演示大调色板。

# importing the modules 
from bokeh.plotting import figure, output_file, show 
from bokeh.palettes import Greys256, Inferno256, Magma256, Plasma256
from 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 palette
graph.vbar(x = [i for i in range(256)], 
           top = [20] * 256,
           bottom = [18] * 256,
           width = 1,
           color = Greys256)
  
# demonstrating the Inferno256 palette
graph.vbar(x = [i for i in range(256)], 
           top = [17] * 256,
           bottom = [15] * 256,
           width = 1,
           color = Inferno256)
  
# demonstrating the Magma256 palette
graph.vbar(x = [i for i in range(256)], 
           top = [14] * 256,
           bottom = [12] * 256,
           width = 1,
           color = Magma256)
  
# demonstrating the Plasma256 palette
graph.vbar(x = [i for i in range(256)], 
           top = [11] * 256,
           bottom = [9] * 256,
           width = 1,
           color = Plasma256)
  
# demonstrating the Viridis256 palette
graph.vbar(x = [i for i in range(256)], 
           top = [8] * 256,
           bottom = [6] * 256,
           width = 1,
           color = Viridis256)
  
# demonstrating the Cividis256 palette
graph.vbar(x = [i for i in range(256)], 
           top = [5] * 256,
           bottom = [3] * 256,
           width = 1,
           color = Cividis256)
  
# demonstrating the Turbo256 palette
graph.vbar(x = [i for i in range(256)], 
           top = [2] * 256,
           bottom = [0] * 256,
           width = 1,
           color = Turbo256)
  
# displaying the model 
show(graph) 

输出 :