重新排列bokeh中的图例项目

2024-09-27 23:24:09 发布

您现在位置:Python中文网/ 问答频道 /正文

在文档中的示例中

from bokeh.io import show
from bokeh.models import ColumnDataSource
from bokeh.palettes import RdBu3
from bokeh.plotting import figure

c1 = RdBu3[2] # red
c2 = RdBu3[0] # blue
source = ColumnDataSource(dict(
    x=[1, 2, 3, 4, 5, 6],
    y=[2, 1, 2, 1, 2, 1],
    color=[c1, c2, c1, c2, c1, c2],
    label=['hi', 'lo', 'hi', 'lo', 'hi', 'lo']
))

p = figure(x_range=(0, 7), y_range=(0, 3), plot_height=300, tools='save')

# Note legend field matches the column in `source`
p.circle( x='x', y='y', radius=0.5, color='color', legend='label', source=source)
show(p)

有没有一种方法可以将图例中项目的顺序更改为('lo'、'hi')而不是('hi'、'lo'),而不必重新排列所有原始数组中的项目?在

enter image description here


Tags: fromimportsourceloshowbokehrangehi
1条回答
网友
1楼 · 发布于 2024-09-27 23:24:09

此解决方案允许完成此任务:

from math import nan

from bokeh.io import show
from bokeh.models import ColumnDataSource
from bokeh.palettes import RdBu3
from bokeh.plotting import figure

c1 = RdBu3[2] # red
c2 = RdBu3[0] # blue
source = ColumnDataSource(dict(
    x=[nan, nan, 1, 2, 3, 4, 5, 6],
    y=[nan, nan, 2, 1, 2, 1, 2, 1],
    color=[c2, c1, c1, c2, c1, c2, c1, c2],
    label=['lo', 'hi', 'hi', 'lo', 'hi', 'lo', 'hi', 'lo']
))

p = figure(x_range=(0, 7), y_range=(0, 3), plot_height=300, tools='save')

# Note legend field matches the column in `source`
p.circle( x='x', y='y', radius=0.5, color='color', legend='label', source=source)
show(p)

相关问题 更多 >

    热门问题