How to add data labels to a bar chart in Bokeh?

后端 未结 2 1704
独厮守ぢ
独厮守ぢ 2020-12-10 05:34

In the Bokeh guide there are examples of various bar charts that can be created. http://docs.bokeh.org/en/0.10.0/docs/user_guide/charts.html#id4

This code will creat

2条回答
  •  醉话见心
    2020-12-10 05:49

    Use Labelset

    Use Labelset to create a label over each individual bar

    In my example I'm using vbar with the plotting interface, it is a little bit more low level then the Charts interface, but there might be a way to add it into the Bar chart.

    from bokeh.palettes import PuBu
    from bokeh.io import show, output_notebook
    from bokeh.models import ColumnDataSource, ranges, LabelSet
    from bokeh.plotting import figure
    output_notebook()
    
    source = ColumnDataSource(dict(x=['Áætlaðir','Unnir'],y=[576,608]))
    
    x_label = ""
    y_label = "Tímar (klst)"
    title = "Tímar; núllti til þriðji sprettur."
    plot = figure(plot_width=600, plot_height=300, tools="save",
            x_axis_label = x_label,
            y_axis_label = y_label,
            title=title,
            x_minor_ticks=2,
            x_range = source.data["x"],
            y_range= ranges.Range1d(start=0,end=700))
    
    
    labels = LabelSet(x='x', y='y', text='y', level='glyph',
            x_offset=-13.5, y_offset=0, source=source, render_mode='canvas')
    
    plot.vbar(source=source,x='x',top='y',bottom=0,width=0.3,color=PuBu[7][2])
    
    plot.add_layout(labels)
    show(plot)
    

    You can find more about labelset here: Bokeh annotations

提交回复
热议问题