Creating a Scatter Plot with Bokeh and Python

  • Share this:

Code introduction


This code defines a function that creates and displays a scatter plot. The function takes a DataFrame containing x and y coordinates as input and uses the Bokeh library for plotting.


Technology Stack : Bokeh, Pandas, NumPy

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

def create_scatter_plot(data):
    # Create a ColumnDataSource from the DataFrame
    source = ColumnDataSource(data)
    
    # Create a new plot with a title and axis labels
    p = figure(title="Scatter Plot", x_axis_label='X-axis', y_axis_label='Y-axis')
    
    # Add a scatter renderer to the plot
    p.scatter('x', 'y', source=source)
    
    # Display the plot
    show(p)

# Sample data for demonstration
data = {
    'x': np.random.random(50) * 100,
    'y': np.random.random(50) * 100
}

# Create a DataFrame from the data
df = pd.DataFrame(data)

# Call the function to create and show the scatter plot
create_scatter_plot(df)