Bokeh plotting rectangle

Hello,

I’m trying to plot a set of rectangles with bokeh.plotting, and I’d like to use a gradient of colors in the rectangle instead of an unique color. Is it an easy way to do so, knowing that in order to plot a rectangle you just have to provide the coordinates of the center and width/height ?

Thank you for your answers !

The ability to add fill patterns to things was only recently added, so first off this is only possible at all with Bokeh 1.2 or later. However, as of 1.2, the rect glyph specifically does not have this feature yet. It is available for vbar, hbar, and quad at present. Assuming you can get by with one of those instead of rect, then presently you can add a gradient image as a fill pattern using ImageURLTexture like this:

p.vbar(...
       hatch_pattern=dict(value='image'), 
       hatch_extra={"image": ImageURLTexture(url=url)})

The url can be a public http or https URL, or if you want everything self-contained, then base64 data: URLs also work (there is an online converter here). Since patterns normally repeat I think for a gradient you will simply have to choose images that are big enough to cover the entire area inside the glyphs. You may need to set the repetition property of the texture to turn off repeating explicitly.

Another option that is more flexible but so far not as well documented is to use a CanvasTexture, which allows you to draw arbitrary patterns on the HTML canvas using JavaScript:

custom = CanvasTexture(code="""
    // color, scale, and width parameters are available to refer to
    ctx.beginPath()
    ...
    ctx.fill()
""")

Theres’ probably some work still to be done to make this more useful, e.g. CanvasTexture should accept an args param like CustomJS callbacks so that it can access other models and properties when drawing.