gpt4 book ai didi

python - 如何使用 python Bokeh 绘制圆图 LinearColorMapper

转载 作者:行者123 更新时间:2023-11-28 22:19:08 24 4
gpt4 key购买 nike

使用以下代码,

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)

我可以制作一个圆形图,在其中为物种着色:

enter image description here

但我想做的是根据范围给所有点上色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)

而且在最终想要的情节中,我怎样才能把颜色条显示值的范围和分配的值。像这样:

enter image description here

我正在使用 Python 2.7.13

最佳答案

为了回答你的第一部分,有一个小错字(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)

enter image description here

另请参阅:https://github.com/bokeh/bokeh/blob/master/examples/plotting/file/color_data_map.py

关于python - 如何使用 python Bokeh 绘制圆图 LinearColorMapper,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50013378/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com