Creating a Scatter Plot with Legend Using Bokeh

  • Share this:

Code introduction


This function creates a scatter plot with a legend using the Bokeh library. It accepts two arrays x and y as parameters, which represent the x and y data points on the scatter plot.


Technology Stack : Bokeh, NumPy

Code Type : The type of code

Code Difficulty :


                
                    
import numpy as np
from bokeh.plotting import figure, show
from bokeh.layouts import row

def plot_scatter_with_legend(x, y):
    # Create a new plot with a title and axis labels
    p = figure(title="Scatter Plot with Legend", x_axis_label='X Axis', y_axis_label='Y Axis')
    
    # Add a scatter renderer with legend and line width
    p.scatter(x, y, legend_label="Scatter Points", line_width=2)
    
    # Show the results
    show(p)

# Example usage
x = np.random.rand(50)
y = np.random.rand(50)
plot_scatter_with_legend(x, y)                
              
Tags: