Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 3

Visualizing Data with Matplotlib and Seaborn

This lesson is built with resources from youtube.com, towardsdatascience.com and 1 more site.

Welcome to our third lesson. In our previous session, you mastered the fundamentals of data manipulation with Pandas, learning how to load, inspect, and transform data—skills that directly parallel your expertise with SQL. You now have the ability to take raw data from your PostgreSQL database and shape it into a clean, usable format within a Pandas DataFrame.

Today, we move to the next crucial step in the AI Development Life Cycle: turning that processed data into actionable insights through visualization. This lesson addresses the learning outcome of using Matplotlib and Seaborn to visualize data distributions and relationships. For an AI System Architect, this isn't about creating beautiful charts for a presentation; it's a critical reconnaissance mission. Before you design a system, you must understand the terrain. Data visualization provides that high-level map of your data's landscape, revealing patterns, spotting anomalies, and helping you form the hypotheses that will guide your entire modeling strategy.

We will explore the essential plots for exploratory data analysis (EDA), focusing on what each plot tells you and how that information informs downstream decisions in building a production AI system.

Why Visualize? The Architect's View

Summary statistics, like those from the .describe() method you learned, are useful but can be misleading. Visualizations provide a much richer, more intuitive understanding of the data. By visualizing, you can:

  • Understand Distributions: See how values of a feature are spread out. Is the data skewed? Are there multiple peaks? This informs data transformation and feature engineering.
  • Identify Relationships: Discover how different features relate to each other and to the target variable you want to predict (e.g., churn). This is the foundation of feature selection.
  • Detect Anomalies: Spot outliers or data quality issues that might negatively impact model performance and require cleaning.

We'll use two main libraries: Matplotlib, the foundational plotting library in Python, and Seaborn, a high-level library built on top of Matplotlib that produces more statistically informative and aesthetically pleasing plots with less code.


Univariate Analysis: Understanding Single Features

The first step in EDA is to understand each feature in isolation. This is called univariate analysis.

Distributions of Numerical Features: Histograms and KDE Plots

For numerical features, the most common visualizations are histograms and Kernel Density Estimation (KDE) plots.

  • A histogram groups data into bins and shows the frequency of data points in each bin. It gives you a sense of the data's shape, center, and spread.
  • A KDE plot creates a smooth, continuous curve to represent the probability density of the data. It's like a smoothed-out histogram.

The video below gives a practical demonstration of how to create these plots.

Exploratory Data Analysis with Pandas Python

This clip from Rob Mulla's tutorial on EDA provides a concise walkthrough of plotting distributions.

Watch the segment from this point. It covers how to create both histograms and KDE plots in Pandas, which uses Matplotlib under the hood. Note the discussion on changing the number of bins to get a clearer picture of the distribution.

The image below, "Feature distribution by Potability class," shows a powerful application of this concept. For nine different water quality features, it overlays the density plots for potable (safe to drink) and non-potable water. You can immediately see which features have distinctly different distributions for the two classes—these are likely to be strong predictors. For example, the distribution of Sulfate looks very different for potable (blue) versus non-potable (grey) water, suggesting it's a valuable feature.

An excellent example of univariate analysis, where the distribution of each feature is visualized separately for each class of the target variable. This technique makes it easy to spot features that are predictive.

Frequencies of Categorical Features: Bar Plots

For categorical features, you typically want to see the frequency of each category. A bar plot is the standard tool for this. You can easily generate one by using the .value_counts() method you've seen, followed by .plot(kind='bar').


Bivariate and Multivariate Analysis: Exploring Relationships

After understanding individual features, the next step is to explore how they relate to each other.

Numerical vs. Numerical: Scatter Plots

To see the relationship between two numerical variables, you use a scatter plot. Each point on the plot represents a single data record (a row in your DataFrame).

Seaborn makes it easy to create expressive scatter plots. A key parameter is hue, which allows you to color the points based on the values of a third, typically categorical, variable. This lets you visualize the interaction of three variables at once.

This video demonstrates creating scatter plots and introduces the power of the hue parameter.

Exploratory Data Analysis with Pandas Python

This segment continues the EDA tutorial, focusing on visualizing relationships between two features.

Watch from this section on feature relationships. Pay close attention to how a simple scatter plot is enhanced using Seaborn's hue parameter to add a third dimension of information (the year a roller coaster was introduced).

Numerical vs. Categorical: Box and Violin Plots

A very common task in problems like churn prediction is to understand how a numerical feature's distribution differs across categories of your target variable. For example, "Is the distribution of Total_Transaction_Amount different for customers who churned versus those who didn't?"

Box plots and violin plots are perfect for this.

  • A box plot summarizes a distribution using its median (the line in the middle of the box), quartiles (the box itself), and range (the "whiskers"), while also showing outliers as individual points.
  • A violin plot combines a box plot with a KDE plot, showing the full probability distribution on either side of the box.

The following reading provides a practical example of using box plots in a churn analysis context.

Practical Data Analysis with Pandas and Seaborn

This article walks through a practical EDA for a bank churn dataset, which is very similar to the project you'll be undertaking.

Focus on the section that begins with the author's observation about average values being misleading. Then, read about using a histogram and a box plot to compare the distributions of Customer_Age and Credit_Limit for churned vs. not-churned customers. This demonstrates exactly how you'd start looking for predictive features.

The Big Picture: Heatmaps and Pair Plots

From a system architect's perspective, you often need a high-level overview. Two visualizations provide this "30,000-foot view" of your data's numerical features.

  1. Correlation Heatmap: This visualizes the correlation matrix, showing how strongly each numerical feature is linearly related to every other feature. The values range from -1 (perfect negative correlation) to +1 (perfect positive correlation). It's an incredibly efficient way to spot:

    • Features highly correlated with your target variable (potential strong predictors).
    • Features that are highly correlated with each other (multicollinearity), which might be redundant.
  2. Pair Plot: This creates a matrix of plots. For each pair of features, it creates a scatter plot. On the diagonal, it shows the distribution (histogram or KDE) of each individual feature. It's a comprehensive first-pass visualization for a dataset.

The image "Penguin Species Pair Plot" is a perfect example of a Seaborn pair plot.

A pair plot visualizing relationships in the Palmer Penguins dataset. The diagonal shows the distribution of each feature (bill length, body mass, etc.) for each species. The off-diagonal cells show scatter plots of each feature pair, colored by species. This single visualization provides a wealth of information.

The following video shows how to create both pair plots and correlation heatmaps.

Exploratory Data Analysis with Pandas Python

This final practical segment from Rob Mulla's tutorial covers these essential multivariate plots.

Watch the section on pair plots, which demonstrates how to quickly visualize many relationships at once. Then, watch the following section on correlation heatmaps, which shows how to compute and visualize the correlation matrix.


A Practical Walkthrough

Now let's tie all these concepts together. The following article provides an excellent end-to-end example of using these visualization techniques for exploratory data analysis on a churn dataset. This is a direct preview of the kind of work you will do in our first project.

Practical Data Analysis with Pandas and Seaborn

This article serves as a great case study, applying the plots we've discussed to a real-world problem.

Read through the article from the beginning. Observe how the author follows a logical flow: Imports and initial inspection. Checks for missing data and inspects the target variable. Uses groupby for initial numerical comparisons. Visualizes distributions with histograms and box plots to dig deeper. Uses correlation heatmaps and scatter plots to find the most promising features. Notice how each visualization leads to a new insight or question, guiding the analysis forward. This iterative process of questioning and visualizing is the essence of EDA.


Conclusion

In this lesson, you've learned the fundamental techniques for visualizing data. This is not just an academic exercise; it's the primary way you'll develop an intuition for a dataset and begin to form a strategy for modeling.

Key Takeaways:

  • Visualization is Reconnaissance: Before building an AI system, you must map the data landscape. Visualizations like histograms, scatter plots, box plots, and heatmaps are your essential cartography tools.
  • Start Simple, Then Go Deep: Begin by understanding individual features (univariate analysis with histograms and bar plots) before exploring relationships between them (bivariate/multivariate analysis with scatter plots, box plots, etc.).
  • Seaborn is Your Ally: Seaborn provides high-level functions (pairplot, heatmap, boxplot) that allow you to create powerful, informative plots with minimal code, letting you focus on the insights, not the plotting mechanics.
  • Insights Inform Architecture: The patterns you discover (e.g., predictive features, redundant features, outliers) will directly influence your decisions in the subsequent stages of feature engineering, model selection, and data preprocessing.

You now have the foundational skills for the "Data" and "Processing" stages of the AI lifecycle. In our next lesson, we will zoom out and formally outline the key stages of the AI Development Life Cycle. This will provide the overarching framework for our entire course, showing how data preparation, modeling, deployment, and monitoring fit together into a cohesive, production-oriented process.