Univariate Analysis
Distribution plots, histograms, box plots, count plots for individual feature understanding.
Univariate Analysis is a free Learn AI with Python lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn AI with Python learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is Univariate Analysis?
Univariate analysis examines one variable at a time to understand its distribution: center, spread, shape, and unusual values.
The right plot depends on the variable type:
- Numeric → histogram, KDE, box plot, violin plot
- Categorical → count plot (bar chart of frequencies)
Setting Up the Plotting Libraries
We use Matplotlib (plt) and Seaborn (sns). Seaborn sits on top of Matplotlib with nicer defaults for statistical plots.
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
sns.set_theme(style="whitegrid")
df = pd.read_csv("data.csv")Histograms with plt.hist
A histogram buckets numeric values into bins and counts how many fall in each. It reveals the overall shape of a distribution.
The bins argument controls resolution — too few hides structure, too many adds noise.
plt.hist(df["age"], bins=30, edgecolor="black")
plt.xlabel("Age")
plt.ylabel("Count")
plt.title("Age Distribution")
plt.show()KDE Plots — Smooth Density
A kernel density estimate (sns.kdeplot) draws a smooth curve approximating the distribution, which can be easier to read than jagged histogram bars.
Combine both with sns.histplot(..., kde=True) to overlay the smooth curve on the bars.
sns.kdeplot(df["age"], fill=True)
plt.show()
sns.histplot(df["age"], bins=30, kde=True)
plt.show()Detecting Skew
Skew measures asymmetry. A long tail to the right is right (positive) skew; a long left tail is left (negative) skew.
Income, prices, and counts are usually right-skewed. You can quantify it with df[col].skew() — values far from 0 indicate skew.
print(df["income"].skew()) # e.g. 2.3 -> strong right skew
sns.histplot(df["income"], bins=40, kde=True)
plt.show()Detecting Bimodality
A bimodal distribution has two peaks, often a sign that two distinct groups are mixed together (e.g. two product types in one price column).
KDE plots make bimodality obvious — look for two humps. It is a hint that a hidden categorical variable may explain the split.
sns.kdeplot(df["price"], fill=True)
plt.title("Two peaks suggest two subpopulations")
plt.show()Box Plots with sns.boxplot
A box plot shows the median (center line), the interquartile range (IQR, the box), and whiskers extending to ~1.5x IQR. Points beyond the whiskers are flagged as outliers.
sns.boxplot(x=df["income"])
plt.title("Income — box plot")
plt.show()Outliers and the IQR Rule
The box plot uses the IQR rule: a point is an outlier if it lies below Q1 - 1.5*IQR or above Q3 + 1.5*IQR.
You can compute the bounds yourself to filter or cap extreme values.
q1 = df["income"].quantile(0.25)
q3 = df["income"].quantile(0.75)
iqr = q3 - q1
low, high = q1 - 1.5*iqr, q3 + 1.5*iqr
outliers = df[(df["income"] < low) | (df["income"] > high)]
print(len(outliers), "outliers")Violin Plots with sns.violinplot
A violin plot combines a box plot with a mirrored KDE. The width at each height shows density, so you see shape and summary statistics at once.
Violins are great for revealing skew or bimodality that a plain box plot would hide.
sns.violinplot(x=df["age"])
plt.title("Age — violin plot")
plt.show()Count Plots for Categorical Variables
For categorical columns, use sns.countplot — a bar chart of category frequencies. It is the visual form of value_counts().
Order bars by frequency for readability with the order argument.
order = df["city"].value_counts().index
sns.countplot(y="city", data=df, order=order)
plt.title("Records per city")
plt.show()Choosing the Right Univariate Plot
Quick decision guide:
- Shape of a numeric column → histogram or KDE
- Outliers and quartiles → box plot
- Shape + summary together → violin plot
- Category frequencies → count plot
Quick Check: Plot Choice
You want to see the median, quartiles, and outliers of a single numeric column.
Recap: Univariate Analysis
You learned to inspect one variable at a time:
plt.histandsns.kdeplotfor distribution shape.skew()to quantify asymmetry; KDE humps for bimodalitysns.boxplot+ the IQR rule for outlierssns.violinplotfor shape and summary togethersns.countplotfor categorical frequencies
Next we look at relationships between two or more variables.
Frequently asked questions
Is the “Univariate Analysis” lesson free?
Yes — the full text of “Univariate Analysis” is free to read here on the web, and the Learn AI with Python course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn AI with Python course, upgrade to CoddyKit PRO.
What will I learn in “Univariate Analysis”?
Distribution plots, histograms, box plots, count plots for individual feature understanding. You practise Learn AI with Python with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Learn AI with Python?
No prior experience is required. Learn AI with Python on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Univariate Analysis” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Learn AI with Python lesson?
Yes. Every Learn AI with Python lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.