Bokeh Bar Chart Generation with Data Dictionary

  • Share this:

Code introduction


This code defines a function named random_bar_chart that takes a dictionary containing 'x' and 'y' values as an argument and uses the Bokeh library to generate a bar chart and save it as an HTML file.


Technology Stack : Bokeh, ColumnDataSource

Code Type : The type of code

Code Difficulty :


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

    output_file("random_bar_chart.html")

    # Create a ColumnDataSource from the data
    source = ColumnDataSource(data=dict(x=data['x'], y=data['y']))

    # Create a new plot with a title and axis labels
    p = figure(title="Random Bar Chart", x_axis_label='X Axis', y_axis_label='Y Axis')

    # Add a bar renderer to the plot
    p.vbar(x='x', top='y', width=0.9, source=source)

    # Display the plot
    show(p)

# Example data
data = {
    'x': ['A', 'B', 'C', 'D'],
    'y': [10, 20, 15, 5]
}

# Call the function with the example data
random_bar_chart(data)