# main.py
# A demonstration of interactive visualization using Bokeh.
#
# Before running, you may need to install bokeh, pandas, and scikit-learn:
# pip install bokeh pandas scikit-learn
import pandas as pd
from sklearn.datasets import load_iris
from bokeh.plotting import figure, show, output_file
from bokeh.models import HoverTool, ColumnDataSource
print("--- Starting Interactive Visualization Demonstration with Bokeh ---")
# --- Section 1: Load and Prepare the Dataset ---
# We will use the Iris dataset, which has multiple numerical features
# and a categorical target, making it great for visualization.
print("\n--- 1. Loading and Preparing the Iris Dataset ---")
iris = load_iris()
df = pd.DataFrame(data=iris.data, columns=iris.feature_names)
df['species'] = pd.Categorical.from_codes(iris.target, iris.target_names)
# Bokeh works best with its own ColumnDataSource for interactive features.
source = ColumnDataSource(df)
print("Dataset loaded and prepared for Bokeh.")
# --- Section 2: Create an Interactive Scatter Plot ---
# We will create a scatter plot of petal length vs. petal width.
# The plot will include interactive tools for panning, zooming, and hovering.
print("\n--- 2. Generating an Interactive Scatter Plot ---")
try:
# Define the tools for the plot
# HoverTool allows us to display information when the user hovers over a data point.
# The tooltips are defined as a list of (label, @column_name) tuples.
hover = HoverTool(
tooltips=[
("Species", "@species"),
("Petal Length", "@{petal length (cm)}"),
("Petal Width", "@{petal width (cm)}")
]
)
# Create a figure object with tools.
p = figure(
width=800, height=500,
title="Iris Dataset: Petal Length vs. Petal Width",
x_axis_label='Petal Length (cm)',
y_axis_label='Petal Width (cm)',
tools=['pan', 'wheel_zoom', 'box_zoom', 'reset', 'save', hover]
)
# Add a scatter glyph to the plot.
# We use the 'source' object we created earlier.
p.scatter(
x='petal length (cm)',
y='petal width (cm)',
source=source,
legend_field='species', # Color the points by species
size=10,
alpha=0.7
)
# Customize the plot's appearance
p.legend.location = "top_left"
p.legend.title = "Species"
p.legend.click_policy = "hide" # Allows hiding species by clicking the legend
# Define the output file
output_filename = 'interactive_iris_plot.html'
output_file(output_filename, title="Iris Interactive Plot")
print(f"Interactive plot will be saved to '{output_filename}'")
# Show the results
show(p)
except Exception as e:
print(f"An error occurred during visualization: {e}")
print("\n--- End of Demonstration ---")