Practice: rainfall dashboard

A 2×2 figure from the sample files, then export it.

Attach all four sample files (units.csv, prices.csv, rainfall.csv, temps.txt) with Add files. Paste each block as the whole editor — later blocks repeat the load so they still run alone.

Row order is Nairobi, Mombasa, Kisumu, Nakuru, Eldoret.

Goal

Build a 2×2 dashboard and export dashboard.png.

1. Load

import os

print("uploads:", os.listdir("/uploads"))
units = np.loadtxt("units.csv", delimiter=",")
prices = np.loadtxt("prices.csv")
rain = np.loadtxt("rainfall.csv", delimiter=",")
temps = np.loadtxt("temps.txt")
print("units", units.shape, "rain", rain.shape, "temps", temps.shape)

2. Rainfall lines

rain = np.loadtxt("rainfall.csv", delimiter=",")
months = ["Jan", "Feb", "Mar", "Apr"]
fig, ax = plt.subplots()
ax.plot(months, rain[0], marker="o", label="Nairobi")
ax.plot(months, rain[2], marker="o", label="Kisumu")
ax.plot(months, rain[4], marker="o", label="Eldoret")
ax.set_ylabel("mm")
ax.legend()
ax.set_title("Rainfall")
plt.show()

3. Revenue bars

units = np.loadtxt("units.csv", delimiter=",")
prices = np.loadtxt("prices.csv")
revenue = (units * prices).sum(axis=1)
cities = ["Nairobi", "Mombasa", "Kisumu", "Nakuru", "Eldoret"]
fig, ax = plt.subplots()
ax.bar(cities, revenue, color="#1d4f7a")
ax.set_ylabel("revenue")
ax.set_title("Revenue by city")
plt.show()

4. The dashboard

units = np.loadtxt("units.csv", delimiter=",")
prices = np.loadtxt("prices.csv")
rain = np.loadtxt("rainfall.csv", delimiter=",")
temps = np.loadtxt("temps.txt")
revenue = (units * prices).sum(axis=1)
cities = ["Nairobi", "Mombasa", "Kisumu", "Nakuru", "Eldoret"]
months = ["Jan", "Feb", "Mar", "Apr"]

fig, axes = plt.subplots(2, 2, figsize=(9, 7))

axes[0, 0].plot(months, rain[0], marker="o", label="Nairobi")
axes[0, 0].plot(months, rain[2], marker="o", label="Kisumu")
axes[0, 0].plot(months, rain[4], marker="o", label="Eldoret")
axes[0, 0].set_title("Rainfall")
axes[0, 0].legend(fontsize=8)

axes[0, 1].bar(cities, revenue, color="#1d4f7a")
axes[0, 1].set_title("Revenue")
axes[0, 1].tick_params(axis="x", rotation=30)

image = axes[1, 0].imshow(rain, cmap="Blues")
fig.colorbar(image, ax=axes[1, 0], fraction=0.046)
axes[1, 0].set_xticks(range(4), months)
axes[1, 0].set_yticks(range(5), cities)
axes[1, 0].set_title("Rain heatmap")

axes[1, 1].plot(temps[:, 0], label="Nairobi")
axes[1, 1].plot(temps[:, 1], label="Mombasa")
axes[1, 1].plot(temps[:, 2], label="Kisumu")
axes[1, 1].set_title("Daily max °C")
axes[1, 1].legend(fontsize=8)

fig.suptitle("Kenya kiosk dashboard")
fig.tight_layout()
plt.show()

5. Export

import os

units = np.loadtxt("units.csv", delimiter=",")
prices = np.loadtxt("prices.csv")
rain = np.loadtxt("rainfall.csv", delimiter=",")
temps = np.loadtxt("temps.txt")
revenue = (units * prices).sum(axis=1)
cities = ["Nairobi", "Mombasa", "Kisumu", "Nakuru", "Eldoret"]
months = ["Jan", "Feb", "Mar", "Apr"]

fig, axes = plt.subplots(2, 2, figsize=(9, 7))
axes[0, 0].plot(months, rain[0], marker="o", label="Nairobi")
axes[0, 0].plot(months, rain[2], marker="o", label="Kisumu")
axes[0, 0].plot(months, rain[4], marker="o", label="Eldoret")
axes[0, 0].set_title("Rainfall")
axes[0, 0].legend(fontsize=8)
axes[0, 1].bar(cities, revenue, color="#1d4f7a")
axes[0, 1].set_title("Revenue")
axes[0, 1].tick_params(axis="x", rotation=30)
image = axes[1, 0].imshow(rain, cmap="Blues")
fig.colorbar(image, ax=axes[1, 0], fraction=0.046)
axes[1, 0].set_xticks(range(4), months)
axes[1, 0].set_yticks(range(5), cities)
axes[1, 0].set_title("Rain heatmap")
axes[1, 1].plot(temps[:, 0], label="Nairobi")
axes[1, 1].plot(temps[:, 1], label="Mombasa")
axes[1, 1].plot(temps[:, 2], label="Kisumu")
axes[1, 1].set_title("Daily max °C")
axes[1, 1].legend(fontsize=8)
fig.suptitle("Kenya kiosk dashboard")
fig.tight_layout()
fig.savefig("dashboard.png", dpi=120, bbox_inches="tight")
plt.show()
print("uploads:", os.listdir("/uploads"))

Click on dashboard.png.

Extra drills

  • Annotate the wettest April cell on the heatmap (np.argmax(rain[:, -1])).
  • Replace the revenue bars with grouped A/B/C bars from units.csv.
  • Save dashboard.svg as well.
You should see

If a CSV is missing, attach the banner files and run again. Empty Plot panel usually means the script never called plt.show().