Plot uploaded files

loadtxt sample CSVs, then draw bars and lines.

Download the sample files from the banner, then click Add files in the workbench. Comment lines starting with # are skipped by loadtxt.

Goal

List /uploads, load rainfall.csv and units.csv, and draw a line and a bar chart.

See what is attached

import os

print("uploads:", os.listdir("/uploads"))

If that list is empty, attach the files and run again.

Rainfall lines

import os

print("uploads:", os.listdir("/uploads"))
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[1], marker="o", label="Mombasa")
ax.plot(months, rain[2], marker="o", label="Kisumu")
ax.set_ylabel("mm")
ax.legend()
ax.set_title("Rainfall")
plt.show()

Row 0 is Nairobi, 1 Mombasa, 2 Kisumu, 3 Nakuru, 4 Eldoret.

Units as grouped bars

units = np.loadtxt("units.csv", delimiter=",")
cities = ["Nairobi", "Mombasa", "Kisumu", "Nakuru", "Eldoret"]
x = np.arange(len(cities))
width = 0.25
fig, ax = plt.subplots()
ax.bar(x - width, units[:, 0], width, label="A")
ax.bar(x, units[:, 1], width, label="B")
ax.bar(x + width, units[:, 2], width, label="C")
ax.set_xticks(x, cities)
ax.set_ylabel("units")
ax.legend()
ax.set_title("Units by city and product")
plt.show()

Whitespace temps

temps = np.loadtxt("temps.txt")
fig, ax = plt.subplots()
ax.plot(temps[:, 0], marker="o", label="Nairobi")
ax.plot(temps[:, 1], marker="o", label="Mombasa")
ax.plot(temps[:, 2], marker="o", label="Kisumu")
ax.set_xlabel("day")
ax.set_ylabel("°C")
ax.legend()
ax.set_title("Daily max")
plt.show()
Pitfall

FileNotFoundError means the workbench cannot see the file in /uploads. Download from the banner, then Add files.