Random Data Scatter Plot Generation

  • Share this:

Code introduction


This function generates a scatter plot with randomly generated data points for visualization purposes.


Technology Stack : Bokeh, NumPy

Code Type : Graphics drawing

Code Difficulty : Intermediate


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

def generate_random_plot(data_points):
    """
    Generates a random scatter plot with a specified number of data points.
    """
    # Create random x and y data points
    x = np.random.random(data_points)
    y = np.random.random(data_points)
    
    # Create a ColumnDataSource to hold the data
    source = ColumnDataSource(data=dict(x=x, y=y))
    
    # Create a new plot with a title and axis labels
    p = figure(title="Random Scatter Plot", x_axis_label='X axis', y_axis_label='Y axis')
    
    # Add a scatter plot to the plot
    p.scatter('x', 'y', source=source, color='blue', alpha=0.5)
    
    # Display the plot
    show(p)

# Example usage
generate_random_plot(50)                
              
Tags: