Scatter Plot Creation with Bokeh and Hover Tool

  • Share this:

Code introduction


This function creates a scatter plot to visualize the input x and y data points. It uses the Bokeh library to create the chart and adds a hover tool to display detailed information about data points.


Technology Stack : Bokeh, NumPy

Code Type : The type of code

Code Difficulty : Intermediate


                
                    
import numpy as np
import bokeh.plotting as plt
from bokeh.models import ColumnDataSource, HoverTool

def create_scatter_plot(x, y):
    # Create a new plot with a title and axis labels
    p = plt.figure(title="Scatter Plot Example", tools="pan,wheel_zoom,box_zoom,reset")

    # Add a HoverTool to the plot
    hover = HoverTool(
        tooltips=[
            ("x", "$x"),
            ("y", "$y"),
            ("index", "@index"),
            ("value", "@value")
        ]
    )
    p.add_tools(hover)

    # Create a ColumnDataSource from the input data
    data = ColumnDataSource(data=dict(x=x, y=y, value=np.random.rand(len(x),)))

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

    # Show the plot
    plt.show()                
              
Tags: