【问题标题】:Reorder legend items in bokeh重新排列散景中的图例项目
【发布时间】:2018-02-15 10:39:40
【问题描述】:

在文档中的示例中

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') 而不重新排序所有原始数组中的项目?

【问题讨论】:

  • 我尝试按照文档中的建议检查p.legend[0].items,但那里只有一项。
  • 我想你可以找到为什么只有一个图例项here的答案。

标签: python plot legend bokeh legend-properties


【解决方案1】:

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

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)

【讨论】:

  • 这仅适用于这个特定的例子,但有兴趣能够定义以一般方式显示的条目序列,例如用于直方图。
猜你喜欢
  • 1970-01-01
  • 2021-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-09
  • 2019-04-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多