Creating a Scatter Plot with Bokeh in Python

  • Share this:

Code introduction


The code defines a function that generates a scatter plot using the Bokeh library. The plot is displayed in a web browser. The function takes a dictionary containing x and y coordinates as input data.


Technology Stack : Bokeh, ColumnDataSource, figure, show, scatter

Code Type : Function

Code Difficulty : Intermediate


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

def generate_scatter_plot(data):
    # Create a ColumnDataSource from the data
    source = ColumnDataSource(data)

    # Create a new plot with a title and axis labels
    p = figure(title="Scatter Plot", x_axis_label='X', y_axis_label='Y')

    # Add a scatter renderer to the plot
    p.scatter('x', 'y', source=source)

    # Show the results
    show(p)

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

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