python - 如何使用 python Bokeh 绘制圆形图 LinearColorMapper
问题描述
使用以下代码,
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
.
解决方案
为了回答您的第一部分,有一个小错字(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
推荐阅读
- polygon - 有没有办法用剪辑路径(对于元素)制作非常详细的撕裂边缘效果?
- android - Dependency injection using hilt on singleton private constructor class
- php - how to add an html button do each row in a table filled with php data?
- discord - How to make a "set mute role" command in Discord.py?
- c# - 为什么在转换浮点数和字节时,BitConverter 似乎返回不正确的结果?
- java - Using JSOUP to Parse Nested Divs
- android - Java processes keep runnning after I quit applications (VS Code, Android Studio)
- tensorflow - 在 Keras 的 MobileNet 应用程序中替换跨步层
- php - Wordpress/Woocommerce/ Hide Prices When Zero Until Calculation fields can return a value
- python - How can you tell if someone is running a decompiled / de-obfuscated version of your code?