You can download this code by clicking the button below.
This code is now available for download.
This code defines a function that creates a scatter plot using the Bokeh library, and adds hover and box select tools. The input data is two arrays representing the x and y values.
Technology Stack : The code uses the Bokeh library and other Python packages such as NumPy and matplotlib for plotting.
Code Type : The type of code
Code Difficulty :
import numpy as np
import bokeh.plotting as plt
from bokeh.models import ColumnDataSource, HoverTool, BoxSelectTool
def plot_random_scatter(x_values, y_values):
# Create a random scatter plot using Bokeh
fig, ax = plt.subplots()
# Create a ColumnDataSource from the input data
source = ColumnDataSource(data=dict(x=x_values, y=y_values))
# Create a scatter plot
p = ax.scatter('x', 'y', source=source)
# Add a hover tool
hover = HoverTool(
tooltips=[
("index", "$index"),
("(x,y)", "($x, $y)"),
],
formatters={
'index': 'numeral',
'x': 'numeral',
'y': 'numeral',
},
mode='vline'
)
ax.add_tools(hover)
# Add a box select tool
select_tool = BoxSelectTool()
ax.add_tools(select_tool)
# Display the plot
plt.show()
# Example usage
x_values = np.random.random(50) * 100
y_values = np.random.random(50) * 100
plot_random_scatter(x_values, y_values)