bs123
bs123

Reputation: 1213

How do i combine multiple bar charts in bokeh?

I want to plot two bar charts on the same figure.

One will have only positive values, one will have only negative values.

I want the positive bars in green (above the x-axis) and the negative bars in red (below the x-axis)

Questions:

1. Is it possible to do this with the existing highlevel Bar method in the bokeh.charts interface?

2. If not, how can I create a bar chart using the lower level bokeh.plotting interface? (rather than the higher level bokeh.charts interface)

Upvotes: 2

Views: 5039

Answers (2)

user3803051
user3803051

Reputation: 73

Edit: The answer below is a few years out of date. All sorts of bar plots (stacked, grouped, color mapped) are much simpler and easier now. See this section of the user's guide for many examples:

https://docs.bokeh.org/en/latest/docs/user_guide/categorical.html


1. I tried doing multiple bar charts using the highlevel Bar method however, I could not achieve what I wanted and so I used the plotting interface.

2. Is this what you are looking for?

    from bokeh.plotting import figure, output_file, show

    plot = figure(width=600, height=600, x_range=(0,50), y_range=(-10,10))

    plot.quad(top=[10],bottom=[0],left=[1],right=[2], color='green', line_color='black', legend='positive')
    plot.quad(top=[12],bottom=[0],left=[2],right=[3], color='green', line_color='black', legend='positive')
    plot.quad(top=[1],bottom=[0],left=[3],right=[4], color='green', line_color='black', legend='positive')
    plot.quad(top=[2],bottom=[0],left=[4],right=[5], color='green', line_color='black', legend='positive')
    plot.quad(top=[3],bottom=[0],left=[5],right=[6], color='green', line_color='black', legend='positive')
    plot.quad(top=[4],bottom=[0],left=[6],right=[7], color='green', line_color='black', legend='positive')

    plot.quad(top=[-5],bottom=[0],left=[1],right=[2], color='red', line_color='black', legend='negative')
    plot.quad(top=[-6],bottom=[0],left=[2],right=[3], color='red', line_color='black', legend='negative')
    plot.quad(top=[-2],bottom=[0],left=[3],right=[4], color='red', line_color='black', legend='negative')
    plot.quad(top=[-8],bottom=[0],left=[4],right=[5], color='red', line_color='black', legend='negative')
    plot.quad(top=[-9],bottom=[0],left=[5],right=[6], color='red', line_color='black', legend='negative')
    plot.quad(top=[-10],bottom=[0],left=[6],right=[7], color='red', line_color='black', legend='negative')

    output_file('test.html')
    show(plot)

Upvotes: 1

Denis Gorbachev
Denis Gorbachev

Reputation: 507

You can use two vbar_stack calls:

volume_figure.vbar_stack(["buys"], x='timestamp', width=1.0, color=[colors.turquoise], source=source)
volume_figure.vbar_stack(["sells"], x='timestamp', width=1.0, color=[colors.tomato], source=source)

In general, bar charts are described in detail in the docs section Handling Categorical Data

Upvotes: 1

Related Questions