• Data Science Foundations
  • I PREFACE
  • Welcome to the General Data Science – Visualization (VIZ) Layer
    • 📘 What You’ll Learn
    • 🌟 Turn Data Into Insight
  • II DATA VISUALIZATION
  • 1 What are common data types in Python and R?
    • 1.1 Explanation
    • 1.2 Common Data Types in Python and R
  • 2 How do you inspect variable types in a dataset?
    • 2.1 Explanation
    • 2.2 Python Code
    • 2.3 R Code
  • 3 How do you convert variable types in a dataset?
    • 3.1 Explanation
    • 3.2 Python Code
    • 3.3 R Code
  • 4 What is the difference between categorical and numerical variables?
    • 4.1 Explanation
      • 4.1.1 🔷 Categorical Variables
      • 4.1.2 🔶 Numerical Variables
  • 5 How do you summarize numerical and categorical variables?
    • 5.1 Explanation
    • 5.2 Python Code
    • 5.3 R Code
  • 6 What are the most effective plots for comparing values across categories?
    • 6.1 Explanation
      • 🔹 Common Visualization Types
      • 📌 Choosing the Right Plot
  • 7 How do you visualize category counts using a bar plot?
    • 7.1 Explanation
    • 7.2 Python Code
    • 7.3 R Code
  • 8 How do you compare group distributions using a boxplot?
    • 8.1 Explanation
    • 8.2 Python Code
    • 8.3 R Code
  • 9 How do you compare distribution shape and summary stats using a violin plot?
    • 9.1 Explanation
    • 9.2 Python Code
    • 9.3 R Code
  • 10 How do you visualize overlapping group distributions using a ridge plot?
    • 10.1 Explanation
    • 10.2 Python Code
    • 10.3 R Code
  • 11 How do you display individual data points by category using a swarm plot?
    • 11.1 Explanation
    • 11.2 Python Code
    • 11.3 R Code
  • 12 How do you show raw observations by group using a strip plot?
    • 12.1 Explanation
    • 12.2 Python Code
    • 12.3 R Code
  • 13 How do you show group means and variability using a bar plot?
    • 13.1 Explanation
    • 13.2 Python Code
    • 13.3 R Code
  • 14 How do you show group summaries using a dot plot?
    • 14.1 Explanation
    • 14.2 Python Code
    • 14.3 R Code
  • 15 How do you show frequency patterns using a histogram?
    • 15.1 Explanation
    • 15.2 Python Code
    • 15.3 R Code
  • 16 How do you visualize a smoothed distribution with a density plot?
    • 16.1 Explanation
    • 16.2 Python Code
    • 16.3 R Code
  • 17 How do you visualize two categorical variables with a grouped bar plot?
    • 17.1 Explanation
    • 17.2 Python Code
    • 17.3 R Code
  • 18 How do you visualize trends across ordered groups using a line plot?
    • 18.1 Explanation
    • 18.2 Python Code
    • 18.3 R Code
  • 19 How do you visualize trends for multiple groups using a line plot?
    • 19.1 Explanation
    • 19.2 Python Code
    • 19.3 R Code
  • 20 How do you show overall trend patterns using a smoothed line?
    • 20.1 Explanation
    • 20.2 Python Code
    • 20.3 R Code
  • III PATTERN RECOGNITION AND RELATIONSHIPS
  • 21 How do you visualize patterns and relationships in multivariate data?
    • 21.1 Explanation
      • 21.1.1 Key tools for visualizing relationships
      • 21.1.2 👇 Core Questions Explored in This Section
  • 22 How do you uncover relationships between multiple variables using a pair plot?
    • 22.1 Explanation
    • 22.2 Python Code
    • 22.3 R Code
  • 23 How do you compare distributions across groups using facet plots?
    • 23.1 Explanation
    • 23.2 Python Code
    • 23.3 R Code
  • 24 How do you enhance scatter plots by adding group color and trend lines?
    • 24.1 Explanation
    • 24.2 Python Code
    • 24.3 R Code
  • 25 How do you quantify linear relationships between numerical variables using a correlation heatmap?
    • 25.1 Explanation
    • 25.2 Python Code
    • 25.3 R Code
  • 26 How do you visualize patterns across multiple numeric features using a parallel coordinates plot?
    • 26.1 Explanation
    • 26.2 Python Code
    • 26.3 R Code
  • 27 How do you uncover structure in high-dimensional data using a PCA plot?
    • 27.1 Explanation
    • 27.2 Python Code
    • 27.3 R Code
  • 28 How do you visualize clustering patterns in high-dimensional data using a t-SNE plot?
    • 28.1 Explanation
    • 28.2 Python Code
    • 28.3 R Code
  • 29 How do you explore complex patterns in high-dimensional data using a UMAP plot?
    • 29.1 Explanation
    • 29.2 Python Code
    • 29.3 R Code (UMAP via uwot)
  • 30 How do you visualize simple proportions using a pie chart?
    • 30.1 Explanation
    • 30.2 Python Code
    • 30.3 R Code
  • 31 How do you create a donut chart to show part-to-whole proportions?
    • 31.1 Explanation
    • 31.2 R Code
  • 32 How do you visualize hierarchical part-to-whole relationships using a treemap?
    • 32.1 Explanation
    • 32.2 Python Code — Interactive
    • 32.3 Python Code — Static
    • 32.4 R Code
  • 33 How do you visualize overlaps using a Venn diagram?
    • 33.1 Explanation
    • 33.2 Python Code
    • 33.3 R Code
  • VIZ Summary
    • 🎨 What You’ve Accomplished
    • 📐 What Comes After Visualization?
    • 🚀 Continue Learning with CDI
  • Explore More Guides

Visualizing Data Patterns with Python and R

Q&A 19 How do you visualize trends for multiple groups using a line plot?

19.1 Explanation

A line plot is ideal for visualizing trends over an ordered variable. In this example, we compute the average sepal length for each species across sepal width bins.

This helps reveal group-specific patterns — for example, whether one species has consistently longer sepals as sepal width increases.


19.2 Python Code

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

# Load dataset
df = pd.read_csv("data/iris.csv")

# Bin sepal_width to simulate order
df["width_bin"] = pd.cut(df["sepal_width"], bins=5)

# Group by bin and species, compute mean sepal length
grouped = df.groupby(["width_bin", "species"])["sepal_length"].mean().reset_index()

# Convert bin to string for plotting
grouped["width_bin"] = grouped["width_bin"].astype(str)

# Line plot
plt.figure(figsize=(8, 5))
sns.lineplot(data=grouped, x="width_bin", y="sepal_length", hue="species", marker="o")
plt.title("Average Sepal Length by Sepal Width Bin and Species")
plt.xlabel("Sepal Width Bin")
plt.ylabel("Mean Sepal Length")
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()
/var/folders/m1/0dxpqygn2ds41kxkjgwtftr00000gn/T/ipykernel_75563/1682245456.py:12: FutureWarning: The default of observed=False is deprecated and will be changed to True in a future version of pandas. Pass observed=False to retain current behavior or observed=True to adopt the future default and silence this warning.
  grouped = df.groupby(["width_bin", "species"])["sepal_length"].mean().reset_index()

19.3 R Code

library(dplyr)
library(ggplot2)
library(readr)

# Load dataset
df <- read_csv("data/iris.csv")

# Bin sepal width into 5 intervals
df <- df %>%
  mutate(width_bin = cut(sepal_width, breaks = 5))

# Compute mean sepal length by bin and species
grouped <- df %>%
  group_by(width_bin, species) %>%
  summarise(mean_length = mean(sepal_length), .groups = "drop")

# Line plot
ggplot(grouped, aes(x = width_bin, y = mean_length, group = species, color = species)) +
  geom_line() +
  geom_point() +
  labs(
    title = "Average Sepal Length by Sepal Width Bin and Species",
    x = "Sepal Width Bin",
    y = "Mean Sepal Length"
  ) +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

✅ Takeaway:
Line plots with grouped trends help you compare patterns side by side — especially when studying how one variable behaves across subgroups. Use color or faceting to highlight these comparisons.