A first seaborn plot

scatterplot from a DataFrame, and why plt.show() is required.

Seaborn draws from a DataFrame. You name columns; seaborn maps them onto axes. The last line is always plt.show().

Goal

Draw a scatterplot and a bar chart from the same toy table, and see both in the Plot panel.

A toy table

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"]
print(df.head())
sns.scatterplot(data=df, x="units", y="revenue")
plt.title("Revenue vs units")
plt.show()

data= is the frame. x= and y= are column names, not arrays.

Color by city

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("Hue is city")
plt.show()

Bars

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.barplot(data=df, x="city", y="revenue")
plt.title("Mean revenue by city")
plt.show()

barplot aggregates: each bar is the mean of that city’s rows unless you pass estimator.

Each block is a complete script. The workbench clears the Plot panel on every Run.

Pitfall

Without plt.show(), the Plot panel stays empty. Do not paste Jupyter magics.