散景 - 显示分类范围

时间:2017-05-23 21:17:42

标签: python bokeh

我试图在带有分类轴的散景图中显示箭头。

在下面的代码中,如果我对p.add_layout(Arrow())行进行评论,则图形会按预期显示在x轴上的类别(但没有箭头)。

但是使用p.add_layout(Arrow())行,我没有得到错误但是没有显示数字。

from bokeh.plotting import figure, output_notebook, show
from bokeh.models import Arrow

output_notebook()

xLbl=['a', 'b', 'c']
p = figure(plot_width=600, plot_height=600, x_range=xLbl)

p.circle(x=xLbl, y=[0, 0, 0.7], radius=0.1,
         color=["navy", "yellow", "red"], fill_alpha=0.1)


p.add_layout(Arrow(x_start='a', y_start=0, x_end='b', y_end=0))

show(p)

我在上面的代码段中做错了什么? 如何调试未显示的散景图?

还有另一种方法可以在散景图中绘制箭头吗?是否可以将箭头末端装饰添加到段字形?

通过以下代码段,我得到一个带箭头和自定义标签的图形,但不使用分类范围。 我使用某种索引技巧重新映射图上的位置和自定义刻度格式:

from bokeh.plotting import figure, output_notebook, show
from bokeh.models import Arrow, FuncTickFormatter, Range1d, FixedTicker

output_notebook()

xLbl=['a', 'b', 'c']
x=[0, 1, 2]

p = figure(plot_width=600, plot_height=600, x_range=Range1d(-0.5, 2.5, min_interval=1))

p.circle(x=x, y=[0, 0, 0.7], radius=0.1,
         color=["navy", "yellow", "red"], fill_alpha=0.1)
p.add_layout(Arrow(x_start=0, y_start=0, x_end=1, y_end=0))

p.xaxis.formatter = FuncTickFormatter(code="""
    var labels = %s;
    return labels[tick];
""" % xLbl)
p.xaxis.ticker = FixedTicker(ticks=x)

show(p)

但这似乎有点复杂,不是要走的路......

1 个答案:

答案 0 :(得分:1)

尝试用1和2替换'a'和'b'。 这看起来很好,标签被映射到基础x值。

from bokeh.plotting import figure, output_notebook, show
from bokeh.models import Arrow

output_notebook()

xLbl=['a', 'b', 'c']
p = figure(plot_width=600, plot_height=600, x_range=xLbl)

p.circle(x=xLbl, y=[0, 0, 0.7], radius=0.1,
         color=["navy", "yellow", "red"], fill_alpha=0.1)


p.add_layout(Arrow(x_start=1, y_start=0, x_end=2, y_end=0))

show(p)