Bokeh factor_cmap 不适用于 scatter 但适用于 vbar

问题描述 投票:0回答:1

我正在尝试应用 factor_cmap 将简单数据点映射到散点图。我从一个简单的example开始并尝试修改它并覆盖一个散点图以测试相同的数据和颜色图是否有效:

import pandas as pd
from bokeh.plotting import figure, output_notebook, show
from bokeh.models import ColumnDataSource, Plot, Scatter
from bokeh.transform import factor_cmap
from bokeh import palettes


my_fruits = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']
my_counts = [5, 3, 4, 2, 4, 6]

source = ColumnDataSource(data=dict(my_fruits=my_fruits, my_counts=my_counts))

p = figure(x_range=my_fruits, plot_height=250, toolbar_location=None, title="Fruit Counts")
p.vbar(x='my_fruits', top='my_counts', width=0.9, source=source,
       line_color='white', fill_color=factor_cmap('my_fruits', palette="Spectral6", factors=my_fruits))
p.scatter('my_fruits', 'my_counts', source=source, size=50, fill_color=factor_cmap('my_fruits', palette="Spectral6", factors=my_fruits), marker="dot", legend_field ='my_fruits')
p.xgrid.grid_line_color = None
p.y_range.start = 0
p.y_range.end = 9
p.legend.orientation = "horizontal"
p.legend.location = "top_center"

show(p)

我得到的结果显示在这里: Result of notebook code 颜色映射适用于条形图,但不适用于覆盖在其上的散点图。

我尝试了什么: 我查看了这个问题,这个提问者的问题与他们的数据有关,所以我不知道解决方案是什么。我看到示例数据可以使用散点图和 factor_cmap,但我不明白为什么我的示例不起作用。数据结构似乎有所不同,但我不明白为什么散点图和 vbar 的绘图行为不同。

我的预期:散点图标记遵循类似于垂直条形图的颜色。

发生了什么:尽管垂直条正确跟随颜色图,但所有标记都是相同的颜色。

python pandas charts bokeh
1个回答
0
投票

“点”标记是唯一的,不会响应

fill_color
。那是因为它通常是对其他一些字形的补充,例如
circle_dot
triangle_dot
,在这些情况下,“点”颜色必须与
line_color
相匹配,而不是
fill_color
(否则它将不可见)。为了在任何地方都保持一致,plain
dot
也只使用
line_color
即使它是单独的:

p.scatter('my_fruits', 'my_counts', source=source, size=50, 
          line_color=factor_cmap('my_fruits', palette="Spectral6", factors=my_fruits), 
          marker="dot", legend_field ='my_fruits')

或者,您可以改用“圆圈”标记,它像典型标记一样同时使用

fill_color
line_color

另请注意,

plot_height
已弃用,在 Bokeh 3.0 及更高版本中只需要是
height

© www.soinside.com 2019 - 2024. All rights reserved.