Scatter Plot Generation with Bokeh and Pandas

  • Share this:

Code introduction


This function creates a scatter plot to visualize the relationship between two random variables. It uses Pandas to process the data and Bokeh to generate the graph.


Technology Stack : Pandas, Bokeh

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, LinearAxis, Title

def create_scatter_plot(data):
    # Create a DataFrame from the given data
    df = pd.DataFrame(data)

    # Create a ColumnDataSource from the DataFrame
    source = ColumnDataSource(df)

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

    # Add a scatter renderer to the plot
    p.add_scatter(x='x', y='y', size=10, source=source, color='blue')

    # Add a linear axis
    p.add_layout(LinearAxis(), 'right')
    p.add_layout(LinearAxis(), 'bottom')

    # Show the plot
    show(p)

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