Table vs bar.
Goal
Run every block and look at the Plot panel.
print("Nairobi 21, Mombasa 15, Kisumu 15")
print("A table is fine for three numbers. A bar is better when you want to compare.")df = pd.DataFrame({
"city": ["Nairobi", "Mombasa", "Kisumu", "Nakuru", "Eldoret"],
"units": [21, 15, 15, 18, 12],
"shillings": [2100, 1500, 1480, 1750, 1190],
})
plt.bar(df["city"], df["units"])
plt.title("Compare units")
plt.show()df = pd.DataFrame({
"city": ["Nairobi", "Mombasa", "Kisumu", "Nakuru", "Eldoret"],
"units": [21, 15, 15, 18, 12],
"shillings": [2100, 1500, 1480, 1750, 1190],
})
print(df.to_string(index=False))
plt.barh(df["city"], df["units"])
plt.title("Same data, easier names")
plt.show()df = pd.DataFrame({
"city": ["Nairobi", "Mombasa", "Kisumu", "Nakuru", "Eldoret"],
"units": [21, 15, 15, 18, 12],
"shillings": [2100, 1500, 1480, 1750, 1190],
})
plt.plot(df["city"], df["units"], marker="o")
plt.title("A line is the wrong mark for unordered cities")
plt.show()Pitfall
Always plt.show(). Paste the whole editor.