Fatskills
Practice. Master. Repeat.
Study Guide: TECH **Python for Data Science: Visualizing Distributions, Relationships, and Time Series**
Source: https://www.fatskills.com/data-science/chapter/tech-python-for-data-science-visualizing-distributions-relationships-and-time-series

TECH **Python for Data Science: Visualizing Distributions, Relationships, and Time Series**

By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.

⏱️ ~8 min read

Python for Data Science: Visualizing Distributions, Relationships, and Time Series

A Hyper-Practical, Zero-Fluff Study Guide


1. What This Is & Why It Matters

You’re a data scientist at a retail company. Your boss drops a dataset on your desk: "Why are sales dropping in Q3? Is it pricing, seasonality, or something else?" You could stare at a spreadsheet for hours—or you could visualize distributions, relationships, and trends in 5 minutes and spot the pattern instantly.

This guide covers how to turn raw data into actionable insights using Python’s visualization libraries (matplotlib, seaborn, plotly). You’ll learn: - Distributions: Are sales normally distributed, or are there outliers skewing results? - Relationships: Does price correlate with demand? Is there a hidden interaction between marketing spend and revenue? - Time Series: Are sales declining, or is this just seasonal noise?

Why this matters in production:
- Debugging models: If your ML model performs poorly, visualizing feature distributions can reveal data leakage or skew.
- Stakeholder communication: A well-designed plot can convince executives to act faster than a 50-page report.
- Anomaly detection: Time-series visualizations help spot fraud, system failures, or unexpected trends before they become crises.

Real-world scenario:
You inherit a legacy forecasting script that’s suddenly predicting negative sales. Instead of diving into the code, you plot the historical data and see a structural break (e.g., a new competitor entered the market). Now you know the model needs retraining—not debugging.


2. Core Concepts & Components


? Distributions

  • Histogram (sns.histplot, plt.hist)
  • Definition: Shows the frequency of values in a dataset.
  • Production insight: If your target variable is bimodal, your model might need stratification (e.g., separate models for high/low spenders).

  • Kernel Density Estimate (KDE) (sns.kdeplot)

  • Definition: Smooths a histogram into a probability density curve.
  • Production insight: KDEs reveal hidden clusters (e.g., two distinct customer segments in a single feature).

  • Boxplot (sns.boxplot)

  • Definition: Displays median, quartiles, and outliers in a single view.
  • Production insight: Outliers in a boxplot might indicate data entry errors or fraudulent transactions.

  • Violin Plot (sns.violinplot)

  • Definition: Combines KDE and boxplot to show distribution shape + summary stats.
  • Production insight: Useful for comparing multiple groups (e.g., sales by region).


? Relationships

  • Scatter Plot (sns.scatterplot, plt.scatter)
  • Definition: Plots two variables to reveal correlations.
  • Production insight: A non-linear relationship (e.g., U-shaped) means linear regression will fail.

  • Line Plot (sns.lineplot, plt.plot)

  • Definition: Connects data points to show trends (e.g., stock prices over time).
  • Production insight: Overplotting (too many lines) can hide trends—use transparency (alpha) or aggregation.

  • Heatmap (sns.heatmap)

  • Definition: Visualizes matrix-like data (e.g., correlation matrices).
  • Production insight: A high correlation between features (e.g., age and birth_year) can cause multicollinearity in models.

  • Pair Plot (sns.pairplot)

  • Definition: Grid of scatter plots for all numerical columns.
  • Production insight: Quickly spot redundant features (e.g., height_cm and height_inches).


⏳ Time Series

  • Time Series Line Plot (sns.lineplot, plt.plot)
  • Definition: Plots data points over time (e.g., daily active users).
  • Production insight: Seasonality (e.g., higher sales in December) can be masked by noise—use rolling averages.

  • Autocorrelation Plot (ACF/PACF) (statsmodels.graphics.tsaplots.plot_acf)

  • Definition: Shows how a time series correlates with its past values.
  • Production insight: Helps determine lag order for ARIMA models.

  • Decomposition Plot (statsmodels.tsa.seasonal.seasonal_decompose)

  • Definition: Splits time series into trend, seasonality, and residuals.
  • Production insight: If residuals are not random, your model is missing a key pattern.


3. Step-by-Step Hands-On: Visualizing a Retail Dataset


Prerequisites

  • Python 3.8+ with pandas, matplotlib, seaborn, statsmodels installed.
    bash pip install pandas matplotlib seaborn statsmodels
  • A dataset with sales, price, date, and region (e.g., Kaggle Retail Dataset).


Step 1: Load and Inspect Data

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from statsmodels.tsa.seasonal import seasonal_decompose

# Load data
df = pd.read_csv("retail_data.csv", parse_dates=["date"])
print(df.head())

# Check for missing values
print(df.isnull().sum())

Expected output:


   date       region  price  sales
0 2022-01-01  North   19.99  150
1 2022-01-02  North   24.99  200
...


Step 2: Visualize Distributions

Sales Distribution (Histogram + KDE)

sns.histplot(df["sales"], kde=True, bins=30)
plt.title("Sales Distribution")
plt.show()

What to look for:
- Skewness: If sales are right-skewed, log-transform them for modeling.
- Outliers: Extreme values might be data errors.


Price vs. Sales (Boxplot)

sns.boxplot(data=df, x="region", y="sales")
plt.title("Sales by Region")
plt.show()

What to look for:
- Median differences: Are some regions underperforming? - Outliers: A single store might be skewing results.


Step 3: Explore Relationships

Price vs. Sales (Scatter Plot)

sns.scatterplot(data=df, x="price", y="sales", hue="region", alpha=0.5)
plt.title("Price vs. Sales by Region")
plt.show()

What to look for:
- Non-linear trends: If sales drop after a certain price, consider a piecewise model.
- Heteroscedasticity: If variance increases with price, your model might need weighted regression.


Correlation Heatmap

corr = df[["price", "sales", "discount"]].corr()
sns.heatmap(corr, annot=True, cmap="coolwarm")
plt.title("Correlation Matrix")
plt.show()

What to look for:
- High correlation (e.g., price and discount): Might cause multicollinearity in regression.


Step 4: Analyze Time Series

Daily Sales Over Time

df.set_index("date", inplace=True)
df["sales"].plot(figsize=(12, 6))
plt.title("Daily Sales Over Time")
plt.show()

What to look for:
- Trends: Is sales growing or declining? - Seasonality: Are there repeating patterns (e.g., weekly spikes)?


Decompose Time Series

decomposition = seasonal_decompose(df["sales"].resample("D").mean(), model="additive", period=7)
decomposition.plot()
plt.show()

What to look for:
- Trend: Long-term increase/decrease.
- Seasonality: Weekly/yearly patterns.
- Residuals: Random noise (if not random, your model is missing something).


4. ? Production-Ready Best Practices


? Security

  • Never hardcode API keys in visualization scripts. Use os.environ or python-dotenv.
    python from dotenv import load_dotenv load_dotenv() API_KEY = os.getenv("API_KEY")

? Cost Optimization

  • Avoid overplotting: Use alpha (transparency) or sns.relplot(kind="line") for large datasets.
  • Cache plots: Save figures to disk instead of regenerating them.
    python plt.savefig("sales_trend.png", dpi=300, bbox_inches="tight")

?️ Reliability & Maintainability

  • Use consistent styles: Set a default theme to avoid mismatched plots.
    python sns.set_theme(style="whitegrid", palette="pastel")
  • Label everything: Axes, titles, legends. Future-you will thank present-you.
    python plt.xlabel("Price ($)") plt.ylabel("Sales (Units)") plt.title("Price Elasticity of Demand")

?️ Observability

  • Log plot generation: Track which visualizations are created (useful for debugging).
    python import logging logging.basicConfig(filename="viz.log", level=logging.INFO) logging.info("Generated sales distribution plot")
  • Set alert thresholds: If a plot shows a 3σ outlier, trigger an alert (e.g., Slack notification).


5. ⚠️ Common Mistakes & Traps

Mistake Symptom Fix/Prevention
Ignoring log scales All data points cluster at the bottom of the plot. Use plt.xscale("log") for exponential data.
Overplotting Plot is a solid blob of color. Use alpha=0.1 or sns.stripplot() for large datasets.
Misleading binning Histogram looks like a uniform distribution. Try bins="auto" or sns.histplot(bins=50).
Ignoring seasonality Time series plot looks like noise. Use seasonal_decompose() to separate trend/seasonality.
Correlation ≠ causation Heatmap shows price and sales are correlated. Run an A/B test or causal inference (e.g., DoWhy library).


6. ? Exam/Certification Focus


Typical Question Patterns

  1. "Which plot best shows the relationship between two continuous variables?"
  2. Scatter plot (not bar plot or histogram).

  3. "How do you visualize a time series with daily seasonality?"

  4. Decomposition plot (trend + seasonality + residuals).

  5. "What’s the difference between a histogram and a KDE?"

  6. Histogram: Binned counts.
  7. KDE: Smooth probability density.

  8. "How do you detect outliers in a dataset?"

  9. Boxplot (IQR method) or Z-score (for normal distributions).

⚠️ Trap Distinctions

  • Histogram vs. Bar Plot:
  • Histogram: Shows distribution of a single variable.
  • Bar Plot: Compares categorical data (e.g., sales by region).
  • Line Plot vs. Scatter Plot:
  • Line Plot: Connects ordered data (e.g., time series).
  • Scatter Plot: Shows relationships (no implied order).

Scenario-Based Question

"You’re analyzing customer churn. You have 10,000 rows of data with 50 features. Which visualization should you use to quickly spot patterns?" - ✅ Pair plot (for numerical features) + heatmap (for correlations).
- ❌ 3D scatter plot (hard to interpret).


7. ? Hands-On Challenge

Challenge:
You have a dataset of daily website traffic (date, visitors). Plot the data and answer: 1. Is there a weekly seasonality? 2. Are there any outliers (e.g., traffic spikes)?

Solution:


import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

# Load data
df = pd.read_csv("traffic.csv", parse_dates=["date"])
df.set_index("date", inplace=True)

# Plot
df["visitors"].plot(figsize=(12, 6))
plt.title("Daily Website Traffic")
plt.show()

# Decompose
from statsmodels.tsa.seasonal import seasonal_decompose
decomposition = seasonal_decompose(df["visitors"], model="additive", period=7)
decomposition.plot()
plt.show()

Why it works:
- The line plot shows trends/outliers.
- The decomposition plot separates seasonality (e.g., higher traffic on weekends).


8. ? Rapid-Reference Crib Sheet

Task Code Notes
Histogram sns.histplot(data=df, x="sales") Use kde=True for density curve.
Boxplot sns.boxplot(data=df, x="region", y="sales") Outliers = dots outside whiskers.
Scatter Plot sns.scatterplot(data=df, x="price", y="sales") Add hue="region" for grouping.
Line Plot df["sales"].plot() Use resample("M").mean() for monthly.
Heatmap sns.heatmap(df.corr()) annot=True for values.
Time Series Decomposition seasonal_decompose(df["sales"], period=7) model="additive" or "multiplicative".
Pair Plot sns.pairplot(df) Slow for >10 columns.
KDE Plot sns.kdeplot(data=df, x="sales") Use shade=True for filled area.
⚠️ Log Scale plt.xscale("log") Fixes right-skewed data.
⚠️ Overplotting alpha=0.1 Makes dense plots readable.


9. ? Where to Go Next

  1. Seaborn Tutorial – Official docs with advanced examples.
  2. Python Data Science Handbook – Free book by Jake VanderPlas (Ch. 4 covers visualizations).
  3. Plotly Express Docs – Interactive plots for dashboards.
  4. Kaggle Visualization Course – Hands-on exercises.


ADVERTISEMENT