Data Analysis for Engineers/Module 4

Module 4 · Section 8 of 10

Lesson 4.7 - Geospatial Basics

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

Overview

Plotting site locations on a simple lat/lon scatter is the lightest-weight geospatial visualization - no mapping library required. We'll plot the facilities sized and colored by total annual energy, the same "color-by-value" idea behind a full choropleth (which would need shapefile geometry and a library like geopandas).

Why This Matters (Engineering Context)

A quick site map answers "which parts of the estate does this dataset actually cover?" - an easy check to skip, but one that catches real problems like analyzing only the metro sites when a report claims full coverage.

Code-Along

# One row per site with its coordinates and total-for-the-year energy
site_totals = df.groupby(["site", "lat", "lon"])["energy_kwh"].sum().reset_index()

fig, ax = plt.subplots(figsize=(7, 8))
# a plain scatter of lon (x) vs lat (y). s = marker size, c = value that drives colour.
scatter = ax.scatter(
    site_totals["lon"], site_totals["lat"],
    s=site_totals["energy_kwh"] / 3000,        # scale energy down to a sensible dot size
    c=site_totals["energy_kwh"], cmap="viridis",
    edgecolor="black", alpha=0.85,
)
# label each point next to its marker
for _, row in site_totals.iterrows():
    ax.annotate(row["site"], (row["lon"], row["lat"]), fontsize=8,
                xytext=(5, 5), textcoords="offset points")
ax.set_xlabel("Longitude"); ax.set_ylabel("Latitude")
ax.set_title("Facility Total Annual Energy, 2023\n(marker size & color = total energy)")
plt.colorbar(scatter, label="Total Energy (kWh)")   # colour legend for the c= values
plt.tight_layout(); plt.show()

print(site_totals.sort_values("energy_kwh", ascending=False))

Run it: 5 labeled points scattered around the Luzon area, with marker size and color both scaling with each site's total annual energy - larger, brighter markers are the heavier consumers.

Practice Exercises

  1. Change the colormap (cmap) to "YlOrRd" and see how the visual story changes.
  2. Add a second layer marking any site with energy_kwh total above 5,000,000 with a red star instead of a circle.
  3. Explain in 1-2 sentences why this point map is not a true choropleth.
# Try the practice exercises here

Knowledge Check

  1. What does marker size and color represent in the site map built here?
  2. What's the key difference between this point map and a true choropleth map?
  3. Why is a quick geospatial sanity check useful before drawing conclusions?
Answer key
  1. Each site's total energy for the year
  2. A choropleth fills whole boundary polygons with color, requiring shapefile geometry; a point map only marks locations
  3. It reveals whether the dataset's geographic coverage matches what the analysis claims

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