greydet
greydet

Reputation: 5539

Bokeh - Display arrows with categorical range

I am trying to display arrows in a Bokeh figure with a categorical axis.

In the following code, if I comment the p.add_layout(Arrow()) line, the figure is well displayed as expected with categories on the x axis (but without the arrow).

But with the p.add_layout(Arrow()) line, I get no error but the figure is not displayed.

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)

What am I doing wrong in the above snippet code? How to debug a bokeh figure that does not show up?

Is there another way to plot arrows in a bokeh figure? Is it possible to add an arrow end decoration to the segments glyph?

With the following snippet I get a figure with arrows and custom labels but without using the categorical range. I use some sort of indexing trick to remap the positions on the figure and custom tick formatting:

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)

But this seems to be a bit complicated and not the way to go...

Upvotes: 1

Views: 996

Answers (1)

Anthonydouc
Anthonydouc

Reputation: 3364

Try replace 'a' and 'b' with 1 and 2. That seems to display fine, the labels are mapped to underlying x values.

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)

Upvotes: 1

Related Questions