Visual encoding

Position vs length vs color.

Position vs length vs color.

Goal

Run every block and look at the Plot panel.

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("Length encodes units")
plt.show()
df = pd.DataFrame({
    "city": ["Nairobi", "Mombasa", "Kisumu", "Nakuru", "Eldoret"],
    "units": [21, 15, 15, 18, 12],
    "shillings": [2100, 1500, 1480, 1750, 1190],
})
plt.scatter(np.arange(len(df)), np.zeros(len(df)), s=df["units"] * 20)
plt.xticks(np.arange(len(df)), df["city"])
plt.title("Area is harder to compare than length")
plt.show()
df = pd.DataFrame({
    "city": ["Nairobi", "Mombasa", "Kisumu", "Nakuru", "Eldoret"],
    "units": [21, 15, 15, 18, 12],
    "shillings": [2100, 1500, 1480, 1750, 1190],
})
plt.scatter(np.arange(len(df)), df["units"], c=df["units"], cmap="Blues", s=80)
plt.xticks(np.arange(len(df)), df["city"])
plt.title("Color as a backup encoding")
plt.colorbar(label="units")
plt.show()
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["shillings"])
plt.title("Position + length: shillings by city")
plt.show()
Pitfall

Always plt.show(). Paste the whole editor.