Creating Random Bar Chart with Bokeh

  • Share this:

Code introduction


This function uses the Bokeh library to create a random bar chart. Data is provided through a ColumnDataSource, and the bar chart is drawn using the vbar method. Finally, the generated chart is saved as an HTML file.


Technology Stack : Bokeh, ColumnDataSource, vbar, gridplot, output_file

Code Type : The type of code

Code Difficulty : Advanced


                
                    
def random_bar_chart(data):
    from bokeh.plotting import figure, show
    from bokeh.io import output_file
    from bokeh.layouts import gridplot
    from bokeh.models import ColumnDataSource

    output_file("random_bar_chart.html")

    source = ColumnDataSource(data=dict(x=data['x'], y=data['y']))
    p = figure(title="Random Bar Chart", x_axis_label='Categories', y_axis_label='Values')
    p.vbar(x='x', top='y', width=0.9, source=source, color='blue')

    show(p)

# Sample data for the bar chart
data = {
    'x': ['Category A', 'Category B', 'Category C', 'Category D'],
    'y': [10, 20, 15, 5]
}

# Function call
random_bar_chart(data)