Comparison

Grouped bars.

Grouped bars.

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("Units by city")
plt.show()
cities = ["Nairobi", "Mombasa", "Kisumu"]
mango = [12, 7, 11]
soda = [9, 8, 4]
x = np.arange(len(cities))
w = 0.35
plt.bar(x - w / 2, mango, w, label="mango")
plt.bar(x + w / 2, soda, w, label="soda")
plt.xticks(x, cities)
plt.legend()
plt.title("Grouped bars")
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"], color="#c45c26")
plt.title("Shillings by city")
plt.show()
print("Grouped bars beat two separate charts when the question is mango vs soda in each city.")
Pitfall

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