sns.scatterplot is the axes-level scatter. Pass data, then column names. Use alpha when points overlap.
Goal
Draw a scatter, fade overlapping points, and map a numeric column onto size.
Basic
df = pd.DataFrame(
{
"city": ["Nairobi", "Nairobi", "Mombasa", "Mombasa", "Kisumu", "Kisumu"] * 2,
"product": ["A", "B"] * 6,
"units": [12, 7, 9, 4, 11, 3, 10, 8, 6, 5, 14, 2],
"price": [10.5, 22.0, 10.5, 22.0, 10.5, 22.0] * 2,
}
)
df["revenue"] = df["units"] * df["price"]
sns.scatterplot(data=df, x="units", y="revenue", hue="city")
plt.title("Revenue vs units")
plt.show()Alpha
rng = np.random.default_rng(0)
n = 80
df = pd.DataFrame(
{
"city": rng.choice(["Nairobi", "Mombasa", "Kisumu"], size=n),
"units": rng.integers(1, 20, size=n),
"price": rng.choice([10.5, 22.0], size=n),
}
)
df["revenue"] = df["units"] * df["price"] + rng.normal(0, 8, size=n)
sns.scatterplot(data=df, x="units", y="revenue", hue="city", alpha=0.6)
plt.title("alpha=0.6")
plt.show()Size channel
rng = np.random.default_rng(1)
n = 60
df = pd.DataFrame(
{
"units": rng.integers(1, 20, size=n),
"price": rng.choice([10.5, 22.0, 31.0], size=n),
}
)
df["revenue"] = df["units"] * df["price"]
sns.scatterplot(data=df, x="units", y="price", size="revenue", sizes=(30, 180))
plt.title("Larger points sold more")
plt.show()Axes-level functions return a matplotlib Axes. You can still call plt.title and plt.xlabel after them.
You should see
Several small scatters side by side belong in Relplot.