Data Analysis for Engineers/Module 4

Module 4 · Section 9 of 10

Lesson 4.8 - Interactive Charts with Plotly

Target: ~9 min read - 20 min hands-on

Overview

plotly.express builds interactive charts - hover tooltips, zoom, pan - with a syntax almost as concise as matplotlib. Interactivity matters most when presenting to someone who wants to explore the data. We'll build an interactive field drive-test track: signal strength measured along a route.

Why This Matters (Engineering Context)

Whether it's a telecom drive test, a mobile air-quality survey, an asset-inspection route, or a delivery run, an interactive track lets a client hover over each point to see the measured value - far more useful in a live presentation than a static path.

Code-Along

import plotly.express as px   # concise API for INTERACTIVE charts (hover / zoom / pan)

np.random.seed(31)
n_points = 15
route_time = pd.date_range("2026-04-10 08:00", periods=n_points, freq="3min")
# a route drifting NE, with small random wander added to each coordinate
lat = np.linspace(14.55, 14.70, n_points) + np.random.normal(0, 0.004, n_points)
lon = np.linspace(121.00, 121.10, n_points) + np.random.normal(0, 0.004, n_points)
# cumsum of small steps = a slowly wandering signal; np.clip keeps it in a plausible band
rssi_dbm = np.clip(-70 + np.cumsum(np.random.normal(0, 3, n_points)), -110, -55)

drive_test = pd.DataFrame({"time": route_time, "lat": lat, "lon": lon,
                           "rssi_dbm": np.round(rssi_dbm, 1)})

# px.line_geo draws lat/lon on a map. hover_name / hover_data set the tooltip contents.
fig = px.line_geo(
    drive_test, lat="lat", lon="lon", hover_name="time",
    hover_data={"rssi_dbm": True, "lat": ":.3f", "lon": ":.3f"},
    title="Field Drive Test - Signal Strength Along Route (hover for RSSI)",
)
# update_traces tweaks the drawn series; here colour the markers by RSSI
fig.update_traces(mode="lines+markers",
                  marker=dict(size=9, color=drive_test["rssi_dbm"],
                              colorscale="RdYlGn", showscale=True))
# update_geos frames the map: region, centre, zoom (projection_scale), country borders
fig.update_geos(scope="asia", center=dict(lat=14.62, lon=121.05),
                projection_scale=45, showcountries=True)
fig.show()
print(drive_test)

Run it: an interactive map renders showing the route, colored by signal strength (RSSI), with hover tooltips showing the exact time, latitude, longitude, and RSSI at each point. In the notebook you can pan and zoom directly on the chart.

Practice Exercises

  1. Add a quality column classifying each point as "Good" (RSSI >= -85) or "Weak" (< -85), and color the track by category instead of by continuous RSSI.
  2. Build a second interactive chart with px.scatter() (not geographic) plotting rssi_dbm over time, with lat/lon on hover.
  3. Export the figure to a standalone HTML file with fig.write_html("drive_test.html") - why is that useful for sharing with a non-technical stakeholder?
# Try the practice exercises here

Knowledge Check

  1. What does plotly.express add over static matplotlib charts?
  2. Name one field where an interactive measured-value-along-a-route chart is useful.
  3. What's one advantage of exporting a Plotly chart to standalone HTML?
Answer key
  1. Interactivity - hover tooltips, zoom, pan - without much more code
  2. Telecom drive testing, mobile environmental surveys, asset-inspection routes, delivery/logistics
  3. It opens in any browser, with no Python installation on the recipient's end

42 / 63 sections · Course home · Join the coaching cohort