如何使用python Bokeh在LinearColorMapper上绘制圆

如何使用python Bokeh在LinearColorMapper上绘制圆,python,plot,bokeh,Python,Plot,Bokeh,使用以下代码 from bokeh.plotting import figure, show, output_file from bokeh.sampledata.iris import flowers colormap = {'setosa': 'red', 'versicolor': 'green', 'virginica': 'blue'} colors = [colormap[x] for x in flowers['species']] p = figure(title = "Ir

使用以下代码

from bokeh.plotting import figure, show, output_file
from bokeh.sampledata.iris import flowers

colormap = {'setosa': 'red', 'versicolor': 'green', 'virginica': 'blue'}
colors = [colormap[x] for x in flowers['species']]

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

p.circle(flowers["petal_length"], flowers["petal_width"],
         color=colors, fill_alpha=0.2, size=10)

output_file("iris.html", title="iris.py example")

show(p)
我可以画一个圆形图,在这里我给物种上色:

但我想做的是根据颜色的范围给所有点上色 花瓣长度中的值

我尝试了此代码,但失败:

from bokeh.models import LinearColorMapper
exp_cmap = LinearColorMapper(palette='Viridis256', low = min(flowers["petal_length"]), high = max(flowers["petal_length"]))

p.circle(flowers["petal_length"], flowers["petal_width"], 
         fill_color = {'field'  : flowers["petal_lengh"], 'transform' : exp_cmap})

output_file("iris.html", title="iris.py example")

show(p)
同样在最终想要的图中,我怎样才能把颜色条 显示值的范围和指定的值。大概是这样的:


我正在使用
python2.7.13
回答您的第一部分,有一个小的打字错误(
petal_lengh
而不是
petal_length
),但更重要的是,使用
bokeh.ColumnDataSource
将解决您的问题(我尝试在没有
CDS
的情况下执行此操作,只得到了列错误):


另请参见:

colormapper转换引用列名,不接受数据的实际文字列表。因此,所有数据都需要位于Bokeh
ColumDataSource
中,并且绘图函数都需要引用列名。幸运的是,这很简单:

p.circle("petal_length", "petal_width", source=flowers, size=20,
         fill_color = {'field': 'petal_length', 'transform': exp_cmap})

此处记录了绘图区域外图例的说明:

p.circle("petal_length", "petal_width", source=flowers, size=20,
         fill_color = {'field': 'petal_length', 'transform': exp_cmap})