散景不显示任何情节后添加调色板

时间:2018-11-19 08:25:22

标签: python bokeh

我想用 bokeh 画一个圆,这个圆的颜色取决于DataFrame的一列。但是我有一个空的情节。如果我没有为 p.circle 指定颜色参数,它将很好地工作。

这是代码,您可以复制并粘贴并运行它。

from bokeh.plotting import figure, output_file, show
from bokeh.models import ColumnDataSource, CategoricalColorMapper
from bokeh.palettes import Spectral11
import pandas as pd

df = pd.DataFrame({
    'price':[10,15,20,25,30],
    'action':[0,1,0,2,3],
    'sign':[0,-1,0,1,-1]
})

source = ColumnDataSource(data=dict(
    index=df.index,
    price=df.price,
    action=df.action,
    sign=df.sign
))

color_mapper = CategoricalColorMapper(factors= [str(i) for i in list(df.sign.unique())], palette=Spectral11)

p = figure(plot_width=800, plot_height=400)

# this works fine
p.circle('index', 'price', radius=0.2 , source=source) 

# this don't work
p.circle('index', 'price', radius=0.2 , color={'field':'sign',  'transform':color_mapper}, source=source)

show(p)

1 个答案:

答案 0 :(得分:0)

ColumnDataSource获取一些信息以及从其他来源获取其他信息时,Bokeh不喜欢它。这对我有用(在笔记本中):

from bokeh.plotting import figure, output_notebook, show
from bokeh.models import ColumnDataSource, CategoricalColorMapper
from bokeh.palettes import Spectral11
import pandas as pd

output_notebook()
df = pd.DataFrame({
    'price':[10,15,20,25,30],
    'action':[0,1,0,2,3],
    'sign':[0,-1,0,1,-1],
})

source = ColumnDataSource(data=dict(
    index=df.index,
    price=df.price,
    action=df.action,
    sign=df.sign,
    color=[Spectral11[i+1] for i in df.sign]
))

p = figure(plot_width=800, plot_height=400)

# this don't work
p.circle('index', 'price', radius=0.2 ,
         color='color',
         source=source)

show(p)