Scatter Plot Generation with Bokeh

  • Share this:

Code introduction


This function creates a scatter plot to show the relationship between two sets of data. It first generates some sample data using Pandas and NumPy, then passes these data to `ColumnDataSource` in the Bokeh library, and finally creates a scatter plot using Bokeh and displays it.


Technology Stack : Bokeh, Pandas, NumPy

Code Type : Data visualization

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 data
    source = ColumnDataSource(data=dict(x=data['x'], y=data['y']))

    # 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, color='blue', alpha=0.6)

    # Show the results
    show(p)

# Sample data for the scatter plot
sample_data = {
    'x': np.random.rand(50),
    'y': np.random.rand(50)
}

# Call the function with the sample data
create_scatter_plot(sample_data)