使用相同的数据,某些调色板会引发错误,而某些调色板会起作用。
from bokeh.io import show, output_file
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure
from bokeh.transform import factor_cmap
from bokeh.palettes import Spectral6, Dark2
output_file("colormapped_dots.html")
cats = ['A', 'A', 'B', 'B', 'C', 'C']
x = [5, 3, 4, 2, 4, 6]
y = x
factors = list(set(cats))
source = ColumnDataSource(data=dict(cats=cats, x=x, y=y))
这个有效,
p = figure()
p.circle('x', 'y', size=10,
color=factor_cmap('cats', palette=Spectral6, factors=factors),
source=source)
show(p)
这一个返回错误,
p = figure()
p.circle('x', 'y', size=10,
color=factor_cmap('cats', palette=Dark2, factors=factors),
source=source)
show(p)
ValueError: expected an element of Seq(Color), got {3: ['#1b9e77', '#d95f02', '#7570b3'], 4: ['#1b9e77', '#d95f02', '#7570b3', '#e7298a'], 5: ['#1b9e77', '#d95f02', '#7570b3', '#e7298a', '#66a61e'], 6: ['#1b9e77', '#d95f02', '#7570b3', '#e7298a', '#66a61e', '#e6ab02'], 7: ['#1b9e77', '#d95f02', '#7570b3', '#e7298a', '#66a61e', '#e6ab02', '#a6761d'], 8: ['#1b9e77', '#d95f02', '#7570b3', '#e7298a', '#66a61e', '#e6ab02', '#a6761d', '#666666']}
这些调色板之间有什么区别?我们如何让'Dark2'工作?
答案 0 :(得分:2)
显然,一些散景调色板是列表,其他是调色板。
print(type(Spectral6))
print(type(Dark2))
<class 'list'>
<class 'dict'>
Dark2 dict实际上是一组调色板,以每个调色板中的颜色数量为基础:
{3: ['#1b9e77', '#d95f02', '#7570b3'],
4: ['#1b9e77', '#d95f02', '#7570b3', '#e7298a'],
5: ['#1b9e77', '#d95f02', '#7570b3', '#e7298a', '#66a61e'],
6: ['#1b9e77', '#d95f02', '#7570b3', '#e7298a', '#66a61e', '#e6ab02'],
7: ['#1b9e77',
'#d95f02',
'#7570b3',
'#e7298a',
'#66a61e',
'#e6ab02',
'#a6761d'],
8: ['#1b9e77',
'#d95f02',
'#7570b3',
'#e7298a',
'#66a61e',
'#e6ab02',
'#a6761d',
'#666666']}
因此需要像这样使用:
pal = Dark2[3]
factor_cmap('cats', palette=pal, factors=factors)
尽管列表类型调色板可以直接发送到“调色板”arg,只要它们包含的颜色至少与因子一样多。
factor_cmap('cats', palette=Spectral6, factors=factors)