Creating a Random Bar Chart with Bokeh

  • Share this:

Code introduction


This code defines a function that uses the Bokeh library to create a bar chart based on random data. It first generates random data, then creates a bar chart and displays it.


Technology Stack : Bokeh library, random data generation, bar chart creation, ColumnDataSource, figure, vbar renderer

Code Type : The type of code

Code Difficulty : Intermediate


                
                    
def create_random_bar_chart(data):
    from bokeh.plotting import figure, show
    from bokeh.models import ColumnDataSource
    import numpy as np

    # Generate random data
    x = np.random.rand(len(data))
    y = np.random.rand(len(data))

    # Create a ColumnDataSource to feed data to the plot
    source = ColumnDataSource(data=dict(x=x, y=y))

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

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

    # Show the results
    show(p)