Search code examples
pythonbar-chartaxis-labelsbokeh

Bokeh Plot with a nominal or ordinal axis type


Edit: The code in the original question refers to a version of Bokeh that is years out of date. But the answer below has been updated to answer the same question for modern versions of Bokeh


Bokeh Plot with a nominal axis type

from bokeh.plotting import *
from bokeh.objects import *
output_notebook()

label = ['United States', 'Russia', 'South Africa', 'Europe (average)', 'Canada', 'Austalia', 'Japan']
number = [1, 2, 3, 4, 5, 6, 7]
value = [700, 530, 400, 150, 125, 125, 75]
yr = Range1d(start=0, end=800)

figure(y_range=yr)
rect(number, [x/2 for x in value] , width=0.5, height=value, color = "#ff1200")
show()

I would like to label the bars with the regions in a Bokeh Plot bar chart. How can I plot a chart with classes (nominal or ordinal)? See the example http://en.wikipedia.org/wiki/File:Incarceration_Rates_Worldwide_ZP.svg.

NOTE: I'm using Python v.2.7.6 and IPython v.1.2.1.


Solution

  • You need to pass the label as x_range=label and use p.xaxis.major_label_orientation to set up label orientation...

    from bokeh.plotting import figure
    from bokeh.io import output_file, show
    
    output_file("bar.html")
    
    label = ['United States', 'Russia', 'South Africa', 'Europe (average)', 'Canada', 'Austalia', 'Japan']
    value = [700, 530, 400, 150, 125, 125, 75]
    
    p = figure(x_range=label, y_range=(0, 800))
    p.xaxis.major_label_orientation = np.pi/4   # radians, "horizontal", "vertical", "normal"
    
    p.vbar(x=label, top=value , width=0.5, color = "#ff1200")
    
    show(p)
    

    enter image description here

    Cheers.