Relationships

Scatter.

Scatter.

Goal

Run every block and look at the Plot panel.

df = pd.DataFrame({
    "units": [12, 7, 9, 4, 11, 3, 14, 8],
    "shillings": [1260, 1540, 945, 880, 1155, 660, 1470, 1760],
    "city": ["Nairobi", "Nairobi", "Mombasa", "Mombasa", "Kisumu", "Kisumu", "Nakuru", "Eldoret"],
})
plt.scatter(df["units"], df["shillings"])
plt.xlabel("units")
plt.ylabel("shillings")
plt.title("Revenue vs units")
plt.show()
df = pd.DataFrame({
    "units": [12, 7, 9, 4, 11, 3, 14, 8],
    "shillings": [1260, 1540, 945, 880, 1155, 660, 1470, 1760],
    "city": ["Nairobi", "Nairobi", "Mombasa", "Mombasa", "Kisumu", "Kisumu", "Nakuru", "Eldoret"],
})
for city, sub in df.groupby("city"):
    plt.scatter(sub["units"], sub["shillings"], label=city)
plt.legend()
plt.title("Color by city")
plt.show()
df = pd.DataFrame({
    "units": [12, 7, 9, 4, 11, 3, 14, 8],
    "shillings": [1260, 1540, 945, 880, 1155, 660, 1470, 1760],
    "city": ["Nairobi", "Nairobi", "Mombasa", "Mombasa", "Kisumu", "Kisumu", "Nakuru", "Eldoret"],
})
plt.scatter(df["units"], df["shillings"], s=df["units"] * 8, alpha=0.7)
plt.title("Size is optional — do not overdo it")
plt.show()
df = pd.DataFrame({
    "units": [12, 7, 9, 4, 11, 3, 14, 8],
    "shillings": [1260, 1540, 945, 880, 1155, 660, 1470, 1760],
    "city": ["Nairobi", "Nairobi", "Mombasa", "Mombasa", "Kisumu", "Kisumu", "Nakuru", "Eldoret"],
})
print(df[["units", "shillings"]].corr())
Pitfall

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