Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to control the z-order of glyphs in Bokeh?

I've been looking through the examples, docs and google and I can't find an example or explanation of how to do this simply...

I'm using the JavaScript library directly, and when I add a glyph to the plot, I just want to specify an order that this glyph should be displayed. I have glyphs that intentionally overlap, and I need to specify that one of them is on top. I believe I can get a list of all the renderers using figure.renderers, and sort this manually, but that seems incredibly tedius and crude...

I've tried using the level parameter, for example like

figure.text(x, y, 'text', {
  text_align: "center",
  text_baseline: "middle",
  text_font_size: {
    value: '8pt'
  },
  level: 'overlay'
});

but I always get the error

bokeh-0.12.0.min.js:3 Uncaught Error: Text.set('level'): level wasn't declared

I've also tried setting level to a numerical value but no luck. I'd like to control the order that glyphs are rendered without having to add them in that order, similar to the way you can do it in MatplotLib.

Is this possible with Bokeh?

like image 899
jms Avatar asked Jul 29 '16 23:07

jms


1 Answers

Some level of control is possible by changing the .level attribute of a patch.

For example, the following code produces a red patch above the blue dots:

from bokeh.io import output_notebook, show
from bokeh.plotting import figure
output_notebook()
p = figure(title='one')
p.scatter([1, 2, 3, 4], [1, 2, 1, 3], size=20)
patch = p.varea(x=[1, 2, 3, 4], y1=[0, 1, 0, 2.5], y2=[1.5, 1.75, 1.5, 3.5], color='red', alpha=0.75)
show(p)

Red patch above the dots

If we want to control the patch z order, we need to modify its level attribute to one of the following: image, underlay, glyph, annotation or overlay. In our case, we need "underlay"

p = figure(title='one')
p.scatter([1, 2, 3, 4], [1, 2, 1, 3], size=20)
patch = p.varea(x=[1, 2, 3, 4], y1=[0, 1, 0, 2.5], y2=[1.5, 1.75, 1.5, 3.5], color='red', alpha=0.75)
patch.level = 'underlay'
show(p)

red patch below the dots

like image 103
Boris Gorelik Avatar answered Oct 20 '22 00:10

Boris Gorelik