Generating Random Bar Charts with Bokeh

  • Share this:

Code introduction


This code defines a function that generates a random bar chart using the Bokeh library. The function accepts a dictionary containing x and y coordinates and colors as input.


Technology Stack : The code uses the Bokeh library and other packages such as NumPy to generate a random bar chart. NumPy is used for generating random data.

Code Type : The type of code

Code Difficulty :


                
                    
def create_random_bar_chart(data):
    """
    This function generates a random bar chart using Bokeh library.
    """
    from bokeh.plotting import figure, show
    from bokeh.models import ColumnDataSource
    import numpy as np

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

    # Create a ColumnDataSource for the data
    source = ColumnDataSource(data=dict(x=x, y=y, color=colors))

    # 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
    p.vbar(x='x', top='y', width=0.5, color='color', source=source)

    # Display the plot
    show(p)

# Example usage
data = {'x': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 'y': [10, 9, 8, 7, 6, 5, 4, 3, 2, 1], 'color': ['blue']*10}
create_random_bar_chart(data)