使用以下代码
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)
我可以制作一个圆形图,用于为物种着色:
但我想做的是根据范围为所有点着色
petal_length
中的值。
我尝试了这段代码,但失败了:
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)
并且在最终所需的情节中,我该如何设置颜色条 显示值的范围和指定的值。像这样:
我正在使用Python 2.7.13
。
答案 0 :(得分:2)
要回答你的第一部分,有一个小错字(petal_lengh
而不是petal_length
),但更重要的是,使用bokeh.ColumnDataSource
将解决你的问题(我试过没有CDS
并且只有列错误:
from bokeh.plotting import figure, show, output_file
from bokeh.sampledata.iris import flowers
from bokeh.models import LinearColorMapper
from bokeh.models import ColumnDataSource
p = figure(title = "Iris Morphology")
p.xaxis.axis_label = "Petal Length"
p.yaxis.axis_label = "Petal Width"
source = ColumnDataSource(flowers)
exp_cmap = LinearColorMapper(palette="Viridis256",
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})
# ANSWER SECOND PART - COLORBAR
# To display a color bar you'll need to import
# the `bokeh.models.ColorBar` class and pass it your mapper.
from bokeh.models import ColorBar
bar = ColorBar(color_mapper=exp_cmap, location=(0,0))
p.add_layout(bar, "left")
show(p)
另请参阅:https://github.com/bokeh/bokeh/blob/master/examples/plotting/file/color_data_map.py
答案 1 :(得分:2)
colormapper变换引用列名,不接受实际的文字数据列表。所以所有数据都需要在Bokeh ColumDataSource
中,并且绘图函数都需要引用列名。幸运的是,这很简单:
p.circle("petal_length", "petal_width", source=flowers, size=20,
fill_color = {'field': 'petal_length', 'transform': exp_cmap})
情节区域外的传说说明如下:
https://bokeh.pydata.org/en/latest/docs/user_guide/styling.html#outside-the-plot-area