Bokeh Scatter Plot Generator with Grid Layout

  • Share this:

Code introduction


This code defines a function that uses the Bokeh library to generate a scatter plot based on random data and displays it in a grid layout.


Technology Stack : Bokeh, NumPy, Pandas

Code Type : The type of code

Code Difficulty : Intermediate


                
                    
import numpy as np
import pandas as pd
from bokeh.plotting import figure, show
from bokeh.models import ColumnDataSource
from bokeh.layouts import gridplot

def generate_random_plot(data):
    """
    Generate a random plot using Bokeh based on the input data.
    """
    # Create a ColumnDataSource from the data
    source = ColumnDataSource(data=dict(x=data['x'], y=data['y']))

    # Create a figure
    p = figure(title="Random Data Plot", tools="pan,wheel_zoom,box_zoom,reset", width=400, height=400)

    # Add a scatter plot
    p.circle('x', 'y', size=10, color='blue', source=source)

    # Create a grid plot
    grid = gridplotchildren([p], plot_width=400, plot_height=400)

    # Display the plot
    show(grid)

# Example data
data = {
    'x': np.random.rand(50),
    'y': np.random.rand(50)
}

# Call the function
generate_random_plot(data)