6 - Data Visualization with Matplotlib and Intro to Pandas — Topics & Learning Outcomes
Module Topics
Introduction to Matplotlib and the pyplot Interface
Introduces the matplotlib library and its pyplot module, explaining how the interface manages the figure and axes objects that underlie every plot.
- What is Matplotlib? — Matplotlib is a comprehensive Python library for creating static, animated, and interactive visualizations. It is the foundational plotting library in the scientific Python ecosystem.
- The pyplot Module — The pyplot module provides a state-based interface to matplotlib, allowing users to create and modify figures with simple function calls. It is typically imported as plt.
- Figures and Axes Objects — In matplotlib, a Figure is the top-level container for all plot elements, while an Axes is the region within the figure where data is plotted. Understanding this hierarchy is essential for building complex visualizations.
- Displaying and Saving Figures — Once a plot is constructed, it can be rendered to the screen with plt.show() or saved to a file with plt.savefig(). Proper use of these functions ensures figures appear and are preserved correctly.
Line Plots and Scatter Plots
Covers the creation and customization of line plots and scatter plots, the two most common chart types for visualizing continuous and relational engineering data.
- Creating a Basic Line Plot — A line plot is created using plt.plot(x, y), which connects data points with line segments. It is ideal for showing trends over a continuous variable such as time or distance.
- Customizing Line Plots — Line plots can be customized with keyword arguments to control color, line width, style, and markers. Meaningful labels and titles improve interpretability.
- Creating a Scatter Plot — A scatter plot is created with plt.scatter(x, y) and displays individual data points without connecting lines. It is useful for exploring correlations and distributions between two variables.
- Axis Limits and Scaling — Controlling the visible range of axes ensures that important data features are not clipped or obscured. Matplotlib provides functions to set both linear and logarithmic scales.
Bar Charts and Histograms
Explains how to create bar charts for categorical comparisons and histograms for examining the distribution of numerical data in engineering contexts.
- Creating a Bar Chart — Bar charts are created with plt.bar(categories, values) and are used to compare discrete groups or categories. They are commonly used to display experimental results across different conditions.
- Annotating Bar Charts — Adding value labels or annotations to bar charts makes them easier to interpret, especially in technical reports. matplotlib provides text placement functions for this purpose.
- Creating a Histogram — Histograms are created with plt.hist(data, bins=n) and display the frequency distribution of a single continuous variable. They are essential for understanding data spread and identifying outliers in engineering measurements.
- Comparing Distributions with Histograms — Multiple datasets can be compared by overlaying histograms on the same axes. Proper use of transparency and color enables effective visual comparison of distributions.
Subplots and Figure Layout
Demonstrates how to create multi-panel figures using subplots, enabling side-by-side comparison of different plots within a single publication-ready figure.
- Creating Subplots with plt.subplots() — The plt.subplots(nrows, ncols) function creates a Figure and a grid of Axes objects in one call. It is the recommended approach for building multi-panel layouts.
- Plotting on Individual Axes — When using subplots, plotting commands are called on individual Axes objects rather than on plt directly. This object-oriented approach provides precise control over each panel.
- Adjusting Layout and Spacing — Subplot panels can overlap or clip labels if spacing is not adjusted. matplotlib provides tools to automatically or manually correct figure layout.
- Using GridSpec for Complex Layouts — For non-uniform subplot grids, matplotlib.gridspec.GridSpec allows plots of different sizes to share a figure. This is useful for dashboard-style engineering figures.
Publication-Quality Figure Styling
Covers techniques for elevating plot aesthetics to meet publication and report standards, including style sheets, custom colors, annotations, and saving high-resolution outputs.
- Using Style Sheets — Matplotlib includes built-in style sheets that instantly apply consistent aesthetics to all figures in a script. Selecting an appropriate style reduces manual customization effort.
- Customizing Colors and Colormaps — Color choices affect both the aesthetic quality and accessibility of a figure. Matplotlib supports named colors, hex codes, RGB tuples, and scientific colormaps.
- Fonts, Tick Marks, and Spines — Fine-tuning typography and axis decorations distinguishes a presentation-ready figure from a draft plot. Matplotlib allows control over font size, tick frequency, and axis border (spine) visibility.
- Saving High-Resolution Figures — Exporting figures at sufficient resolution is critical for print publications and technical reports. The plt.savefig() function supports multiple formats and resolution settings.
Introduction to Pandas: Series and DataFrames
Introduces the pandas library and its two primary data structures — Series and DataFrame — which form the foundation for data manipulation and analysis in Python.
- What is Pandas? — Pandas is a Python library built on NumPy that provides high-performance, easy-to-use data structures for working with labeled and relational data. It is the standard tool for tabular data analysis in Python.
- The Pandas Series — A Series is a one-dimensional labeled array capable of holding data of any type. It behaves like a NumPy array but with an associated index that labels each element.
- The Pandas DataFrame — A DataFrame is a two-dimensional labeled data structure with columns of potentially different types, analogous to a spreadsheet or SQL table. It is the primary pandas object for engineering datasets.
- Inspecting a DataFrame — Pandas provides several methods for quickly exploring the structure and content of a newly loaded dataset. These are essential first steps in any data analysis workflow.
Reading, Filtering, and Sorting Data with Pandas
Demonstrates how to load datasets from files and then select, filter, and sort rows and columns to extract meaningful subsets for engineering analysis.
- Reading Data from Files — Pandas provides read functions that import data from common file formats directly into a DataFrame. Reading CSV files is the most common operation in engineering data workflows.
- Selecting Columns and Rows — Pandas provides multiple methods for selecting subsets of a DataFrame by column name or row index. Mastery of these selection methods is fundamental to data manipulation.
- Filtering Rows with Boolean Conditions — Boolean indexing allows rows to be selected based on conditions applied to one or more columns. This is the standard method for subsetting engineering data by threshold or category.
- Sorting DataFrames — Sorting a DataFrame rearranges rows based on the values in one or more columns. Sorted data is easier to inspect and is required as a preprocessing step for some analyses.
- Adding and Modifying Columns — New columns can be added to a DataFrame through direct assignment, enabling derived variables to be computed from existing data. This is essential for feature engineering and unit conversions.
Descriptive Statistics with Pandas
Covers pandas methods for computing summary statistics, aggregating grouped data, and identifying missing values in engineering datasets.
- Summary Statistics with describe() — The df.describe() method computes count, mean, standard deviation, min, max, and quartiles for all numeric columns simultaneously. It provides a rapid overview of dataset characteristics.
- Aggregation with groupby() — The groupby() method splits a DataFrame into groups based on column values and applies aggregate functions to each group. It is the pandas equivalent of SQL GROUP BY.
- Handling Missing Values — Missing values represented as NaN are common in real engineering datasets from sensor dropouts or incomplete records. Pandas provides methods to detect, remove, or fill missing data.
- Correlation and Relationships — Pandas can compute pairwise correlations between numeric columns, providing a quick measure of linear relationships within an engineering dataset.
- Plotting Directly from Pandas — Pandas DataFrames and Series have built-in plot methods that call matplotlib internally, enabling quick exploratory visualizations with minimal code.
Student Learning Outcomes
By the end of this module, students will be able to:
MO1
Construct line plots, scatter plots, bar charts, and histograms using Matplotlib's pyplot interface with appropriate axis labels, titles, and customization parameters
Level: ApplyType: CognitiveCourse mapping: CO3
MO2
Compose multi-panel publication-quality figures using plt.subplots() and GridSpec with correctly adjusted layout, styling, and high-resolution export settings
Level: CreateType: CognitiveCourse mapping: CO3
MO3
Load a CSV dataset into a Pandas DataFrame and apply column selection, Boolean row filtering, and sorting operations to extract targeted subsets of engineering data
Level: ApplyType: CognitiveCourse mapping: CO1
MO4
Compute descriptive statistics and grouped aggregations on a Pandas DataFrame using describe() and groupby() to summarize engineering dataset characteristics
Level: AnalyzeType: CognitiveCourse mapping: CO3
MO5
Evaluate the distribution and relationships within an engineering dataset by interpreting histograms, correlation values, and grouped summary statistics generated with Matplotlib and Pandas
Level: EvaluateType: CognitiveCourse mapping: CO3
Course Outcomes (reference)
CO1Use variables, arrays and object structures appropriate to solve engineering problems.
CO2Write computer functions to meet specific calculation needs.
CO3Write computer programs to solve mathematical expressions and engineering problems