Random Bar Chart Generator with Bokeh

  • Share this:

Code introduction


This function creates a random bar chart using the Bokeh library. It takes a dictionary as input, generates random x and y values, and then creates a bar chart using these values.


Technology Stack : Bokeh, NumPy

Code Type : Bokeh chart generation

Code Difficulty : Intermediate


                
                    
def random_bar_chart(data, x, y):
    """
    This function creates a random bar chart using the Bokeh library.
    """
    from bokeh.plotting import figure, show
    from bokeh.models import ColumnDataSource

    # Generate random data
    import numpy as np
    x_values = np.random.choice(data.keys(), size=5)
    y_values = np.random.choice(data.values(), size=5)

    # Create a ColumnDataSource
    source = ColumnDataSource(data=dict(x=x_values, y=y_values))

    # Create a new plot with a title and axis labels
    p = figure(title="Random Bar Chart", x_axis_label=x, y_axis_label=y)

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

    # Show the results
    show(p)

# Example usage
data = {'A': 10, 'B': 20, 'C': 30, 'D': 40, 'E': 50}
random_bar_chart(data, 'Category', 'Value')                
              
Tags: