Equivalent of mpl broken_barh

Hi

I'm using matplotlib's broken_barh( http://matplotlib.org/examples/pylab_examples/broken_barh.html) plots quite extensively in jupyter notebooks at the moment. I'm looking to adopt bokeh for the plots to allow interactivity but I have not found a bokeh way to a broken_barh. i don't think i'm familiar enough with bokeh to roll my own yet.

Iss there a broken_barh equivalentt inN bokeh?

I don’t know if there is a high level plotting command for it, but you can always fallback on the plotting primitives Bokeh provides. In this case ‘quad’ and ‘rect’ are probably your best options.

For example:

import numpy as np
from bokeh.plotting import figure
from bokeh.io import output_notebook, show
from bokeh.palettes import Accent3 as colors

output_notebook()

p = figure(height=300, width=500)

for n, color in enumerate(colors):

bottom = n
top = n+.8
   
left = np.random.randint(2,5,10).cumsum()
right = left[:-1] + np.diff(left) - np.random.randint(0,2,9)
left = left[:-1]

p.quad(top=top, bottom=bottom, left=left, right=right, color=color, legend=color)

show(p)

``

Here is a notebook which shows the result as well:

Regards,
Rutger

···

On Tuesday, November 29, 2016 at 2:14:34 PM UTC+1, [email protected] wrote:

Hi

I’m using matplotlib’s broken_barh( http://matplotlib.org/examples/pylab_examples/broken_barh.html) plots quite extensively in jupyter notebooks at the moment. I’m looking to adopt bokeh for the plots to allow interactivity but I have not found a bokeh way to a broken_barh. i don’t think i’m familiar enough with bokeh to roll my own yet.

Iss there a broken_barh equivalentt inN bokeh?

Actually there are two new glyph methods vbar and hbar that are probably even simpler.

There's nothing built into bokeh that accepts pairs of coordinates like that, Bokeh always takes coordinates as individual lists, separately. But it's straightforward to write your own helper function:

  def broken_barh(plot, endpoints, **kw):
    left, right = zip(endpoints)
    plot.hbar(left=left, right=right, **kw)

Then something like:

  p = figure()
  broken_barh(plot, [(0,1), (2, 4)], y=[10, 20], height=0.5)

Untested, but should be a start.

Thanks,

Bryan

···

On Nov 29, 2016, at 9:02 AM, Rutger Kassies <[email protected]> wrote:

I don't know if there is a high level plotting command for it, but you can always fallback on the plotting primitives Bokeh provides. In this case 'quad' and 'rect' are probably your best options.

For example:

import numpy as np
from bokeh.plotting import figure
from bokeh.io import output_notebook, show
from bokeh.palettes import Accent3 as colors

output_notebook()

p = figure(height=300, width=500)

for n, color in enumerate(colors):
    
    bottom = n
    top = n+.8
        
    left = np.random.randint(2,5,10).cumsum()
    right = left[:-1] + np.diff(left) - np.random.randint(0,2,9)
    left = left[:-1]
    
    p.quad(top=top, bottom=bottom, left=left, right=right, color=color, legend=color)
    
show(p)

Here is a notebook which shows the result as well:
Jupyter Notebook Viewer

Regards,
Rutger

On Tuesday, November 29, 2016 at 2:14:34 PM UTC+1, benatw...@gmail.com wrote:
Hi

I'm using matplotlib's broken_barh( http://matplotlib.org/examples/pylab_examples/broken_barh.html\) plots quite extensively in jupyter notebooks at the moment. I'm looking to adopt bokeh for the plots to allow interactivity but I have not found a bokeh way to a broken_barh. i don't think i'm familiar enough with bokeh to roll my own yet.

Iss there a broken_barh equivalentt inN bokeh?

--
You received this message because you are subscribed to the Google Groups "Bokeh Discussion - Public" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/bokeh/672d6ed1-a3ec-4a41-b31c-7ce8e8fbec03%40continuum.io\.
For more options, visit https://groups.google.com/a/continuum.io/d/optout\.

Thank you. Sorry about the double letters in the last sentence and lack of forward gratitude - my

Thank you. I'll test Rutger's and your tips today.

Ok, looks like I can replicate The Matplotlib pyplot broken_barh pretty closely (bar the annotation) using Bokeh. Thanks!

# Reproducing the Matplotlib broken_barh example in Bokeh....
# (http://matplotlib.org/examples/pylab_examples/broken_barh.html)

import numpy as np
from bokeh.plotting import figure
from bokeh.io import output_notebook, show # I'm using Jupyter notebook
from bokeh.palettes import Accent3 as colors
from bokeh.models import Range1d, FixedTicker, HoverTool,Label
from bokeh.models.formatters import TickFormatter, String, List, Dict, Int
from bokeh import charts, plotting, models

import numpy
import pandas
import bokeh
output_notebook()

p = figure(height=600, width=800)#, y_range=['Bill', 'Jim'])

def bokeh_broken_barh(x, y, facecolors='blue'):
    left = [i[0] for i in x]
    right = [left[idx] + i[1] for idx,i in enumerate(x)]
    p.quad(top=y[0] + y[-1], bottom=y[0], left=left, right=right,fill_color=facecolors)# ['red','blue'])#facecolors)

x_stlen = [(110, 30), (150, 10)]
y_stlen = (10, 9)

bokeh_broken_barh(x_stlen, y_stlen, facecolors='blue')

x_stlen = [(10, 50), (100, 20), (130, 10)]
y_stlen = (20, 9)
ret = bokeh_broken_barh(x_stlen, y_stlen,facecolors=['red', 'yellow', 'green'])

p.set(x_range=Range1d(0,200),y_range=Range1d(5,35))
p.xaxis.axis_label = 'seconds since start'

JS_CODE = """
        _ = require "underscore"
        Model = require "model"
        p = require "core/properties"
        class FixedTickFormatter extends Model
          type: 'FixedTickFormatter'
          doFormat: (ticks) ->
            labels = @get("labels")
            return (labels[tick] ? "" for tick in ticks)
          @define {
            labels: [ p.Any ]
          }
        module.exports =
          Model: FixedTickFormatter
    """

class FixedTickFormatter(TickFormatter):

    labels = Dict(Int, String, help="""
    A mapping of integer ticks values to their labels.
    """)

    __implementation__ = JS_CODE

p.yaxis[0].formatter = FixedTickFormatter(labels={15 : "Bill", 25 : "Jim"})

show(p)