python - 散景:使用 MultiLine 字形时无法生成不同的线条颜色
问题描述
我使用 Bokeh 生成了一个使用滑块更新的多线图。我找不到用不同颜色绘制每条线的方法。我尝试使用 itertools 来遍历调色板,并传递一系列调色板颜色。
这是 itertools 方法(full_source 用于支持使用 CustomJS 的滑块交互):
import itertools
from bokeh.plotting import figure
from bokeh.embed import components
from bokeh.models import CustomJS, ColumnDataSource, Slider
from bokeh.palettes import Category20 as palette
from bokeh.models.glyphs import MultiLine
from bokeh.models.widgets import DataTable, TableColumn
from bokeh.layouts import column, row
from bokeh.io import show
data={'xdata':[[0, 1, 2, 4, 5, 6, 10, 11, 12], [4, 8, 16, 0, 13, 21, -3, 9, 21]],
'ydata':[[4, 8, 16, 0, 13, 21, -3, 9, 21], [0, 1, 2, 4, 5, 6, 10, 11, 12]]}
colors=itertools.cycle(palette[2])
source = ColumnDataSource(data)
full_source = ColumnDataSource(data)
glyph = MultiLine(xs='xdata', ys='ydata', line_color = next(colors))
p = figure(title = None, plot_width = 400, plot_height = 400, toolbar_location = None)
p.add_glyph(source, glyph)
print(glyph.line_color)
show(p)
这给出了两条线,但都具有相同的颜色。 print(glyph.line_color)
仅显示一种颜色通过 - #1f77b4(这是 Category20 调色板中的第一种颜色)
我也尝试使用此处找到的示例:
import itertools
from bokeh.plotting import figure
from bokeh.embed import components
from bokeh.models import CustomJS, ColumnDataSource, Slider
from bokeh.palettes import Spectral11
from bokeh.models.glyphs import MultiLine
from bokeh.models.widgets import DataTable, TableColumn
from bokeh.layouts import column, row
from bokeh.io import show
data={'xdata':[[0, 1, 2, 4, 5, 6, 10, 11, 12], [4, 8, 16, 0, 13, 21, -3, 9, 21]],
'ydata':[[4, 8, 16, 0, 13, 21, -3, 9, 21], [0, 1, 2, 4, 5, 6, 10, 11, 12]]}
my_pallet = Spectral11[0:2]
source = ColumnDataSource(data)
full_source = ColumnDataSource(data)
glyph = MultiLine(xs='xdata', ys='ydata', line_color = my_pallet)
p = figure(title = None, plot_width = 400, plot_height = 400, toolbar_location = None)
p.add_glyph(source, glyph)
print(glyph.line_color)
show(p)
这给出了:
ValueError expected an element of either String, Dict(Enum('expr', 'field', 'value', 'transform'), Either(String, Instance(Transform), Instance(Expression), Color)) or Color, got ['#5e4fa2', '#3288bd', '#66c2a5']
如何将调色板中的多种颜色转换为多线图?
解决方案
好的,看起来我没有正确使用 ColumnDataSource。通过将颜色作为数据字典中的附加键:值对传递给 ColumnDataSource,它可以工作。我也可以摆脱 MultiLine 字形对象。
工作代码是:
from bokeh.plotting import figure
from bokeh.embed import components
from bokeh.models import CustomJS, ColumnDataSource, Slider
from bokeh.palettes import Category20 as palette
from bokeh.models.widgets import DataTable, TableColumn
from bokeh.layouts import column, row
from bokeh.io import show
data = {'xs':[[...,...,..,][...,...,...]],'ys':[[...,...,..,][...,...,...]]}
length = len(data)
colors = palette[length]
#because Category20 has a minimum of 3 values, and length may be smaller
while len(colors)>length:
colors.pop()
data['color'] = colors
source = ColumnDataSource(data)
full_source = ColumnDataSource(data)
p = figure(title = None, plot_width = 400, plot_height = 400, toolbar_location = None)
p.multi_line(xs='xdata', ys='ydata', source=source, line_color='color')
推荐阅读
- excel - 使用互操作按列对工作表进行排序
- azure - 在 Azure Function App 中添加或删除主机密钥
- docker - 如何解决“!!模板:conf:2:命令中的意外未关闭操作”
- google-apps-script - 通过函数使用脚本创建文档
- json - 如何使用 slurpfile 和 jq 发送参数?
- oracle - Spark Sql Connection
- docker - 如何为 docker 使用基础镜像并从中构建自己的镜像?
- primary-key - 我们如何在表中创建主键
- angular - MacOs Safari、iOS Safari 和 Chrome 上的 Angular 8 PWA 空白。没有错误
- c# - (Blazor) AZure 中的个人身份验证