如何在Python的散景图中扩展有限的调色板

时间:2018-05-01 05:53:47

标签: python plot bokeh

我有以下代码:

from bokeh.plotting import figure, show, output_file
from bokeh.sampledata.iris import flowers  
from bokeh.models import LinearColorMapper
from bokeh.models import ColumnDataSource 
from bokeh.models import ColorBar 
from bokeh.palettes import Reds9

p = figure(title = "Iris Morphology")
p.xaxis.axis_label = "Petal Length"
p.yaxis.axis_label = "Petal Width"

source = ColumnDataSource(flowers)


# Reverse the color and map it
Reds9.reverse()
exp_cmap = LinearColorMapper(palette=Reds9, 
                             low = min(flowers["petal_length"]), 
                             high = max(flowers["petal_length"]))

p.circle("petal_length", "petal_width", source=source, line_color=None,
        fill_color={"field":"petal_length", "transform":exp_cmap})



bar = ColorBar(color_mapper=exp_cmap, location=(0,0))
p.add_layout(bar, "left")

show(p)

产生以下情节: enter image description here

请注意,我使用Brewer的红色调色板,仅限9种颜色。

from bokeh.palettes import Reds9

如何将其扩展为256?

1 个答案:

答案 0 :(得分:1)

Bokeh的调色板只是一个HTML颜色列表。你可以创建自己的。例如,从https://www.w3schools.com/colors/colors_shades.asp中选择列表:

myReds = [
    '#000000', 
    '#080000', 
    '#100000', 
    '#180000', 
    '#200000', 
    '#280000', 
    '#300000', 
    '#380000', 
    '#400000', 
    '#480000', 
    '#500000', 
    '#580000', 
    '#600000', 
    '#680000', 
    '#700000', 
    '#780000', 
    '#800000', 
    '#880000', 
    '#900000', 
    '#980000', 
    '#A00000', 
    '#A80000', 
    '#B00000', 
    '#B80000', 
    '#C00000', 
    '#C80000', 
    '#D00000', 
    '#D80000', 
    '#E00000', 
    '#E80000', 
    '#F00000', 
    '#F80000', 
    '#FF0000']

然后用myReds替换Reds9:

[...]
exp_cmap = LinearColorMapper(palette=myReds, 
                         low = min(flowers["petal_length"]), 
                         high = max(flowers["petal_length"]))
[...]