Visualizing Distributions: Choosing the Right Plot
Visualization reveals patterns, trends and anomalies that statistics alone hide. The right plot depends on the data type and the question.
# Stacked bar: quality levels split by a binned alcohol category
df["alcohol_level"] = pd.cut(df["alcohol"], bins=3, labels=["Low", "Medium", "High"])
pd.crosstab(df["quality"], df["alcohol_level"]).plot(kind="bar", stacked=True, figsize=(8, 5))
plt.title("Quality by Alcohol Level")
plt.show()
# Pie chart for a simple share of the whole
df["alcohol_level"].value_counts().plot(kind="pie", autopct="%1.1f%%")
plt.ylabel("")
plt.show()
# Scatter plot with a regression line
sns.regplot(x="alcohol", y="density", data=df, scatter_kws={"s": 12})
plt.show()
1. Bar plot
Shows the frequency or proportion of categories.
- Best for: comparing counts across categories, e.g. products sold per category.
- Not suitable: continuous data, or categories with too many distinct values (clutter).
- Example: a marketing team compares purchases per product type over a month.
2. Stacked bar graph
Shows the composition of each main category split into sub-categories.
- Best for: part-to-whole inside groups, e.g. product sales per region.
- Not suitable: too many sub-categories; only the bottom segment is easy to compare.
- Example: a regional sales manager splits sales by product line for each region.
3. Histogram
Groups continuous values into bins; bar height = count in the bin.
- Best for: the frequency distribution of salaries, exam scores or ages.
- Not suitable: small data (bins become noisy); outliers can squash the main bulk.
- Example: distribution of time spent on a website by visitors.
4. Box plot
Summarises min, Q1, median, Q3, max and flags outliers.
- Best for: comparing distributions across groups and spotting outliers, e.g. prices in different markets.
- Not suitable: very small data or data with little variation; it hides multimodality.
- Example: home prices by region for a real-estate analyst.
5. Violin plot
A box plot plus a mirrored density curve.
- Best for: comparing shape and density across several groups.
- Not suitable: only two groups where a box plot is simpler; very small samples (density is unreliable).
- Example: blood-pressure readings by age group.
6. Pie chart
Shows proportions of a whole.
- Best for: few categories (up to about 5), e.g. market share.
- Not suitable: many categories; precise comparison (angles are hard to judge). A sorted bar chart is usually clearer.
- Example: share of each product category in total sales.
7. Correlation heatmap
A colour-coded matrix of correlation coefficients.
- Best for: finding multicollinearity and the variables most related to the target.
- Not suitable: too many variables (unreadable); choose a subset.
- Example: how product quality, customer service and delivery time correlate with overall satisfaction.
8. Scatter plot
Each observation is a dot in a 2D plane.
- Best for: relationships, trends and clusters between two continuous variables.
- Not suitable: categorical variables; non-linear patterns without transformation may be misjudged as "no relation".
- Example: square footage versus price of houses.
Quick chooser
| Data situation | Plot |
|---|---|
| One categorical | Bar, pie (few) |
| One numeric | Histogram, box, violin, KDE |
| Numeric across groups | Box, violin |
| Two numeric | Scatter (+ regression line) |
| Category composition | Stacked / 100% stacked bar |
| Many numeric | Heatmap, pair plot |
| Overplotted scatter (large data) | Hexbin, 2D KDE, alpha transparency |