Skip to main content
Matplotlib is the foundational plotting library for Python. It provides full control over every element of a chart—from axes and labels to grids and legends—allowing you to create publication-quality charts.

1. Introduction and Core Interfaces

Matplotlib offers two main interfaces to create plots:
  1. State-Based Interface (using plt.xxx): The simplest and most common method for single plots. You call functions directly on pyplot, and Matplotlib automatically manages the figure and plot details under the hood.
  2. Object-Oriented Interface (using fig, ax): Recommended for advanced setups, such as creating grid layouts (subplots) or managing multiple charts simultaneously.
For single charts, the State-Based (plt) style is highly preferred due to its simplicity.

Importing Matplotlib

By convention, the plotting module matplotlib.pyplot is imported under the alias plt:

Creating Your First Plot


2. Anatomy and Customization of a Plot

You can easily customize titles, labels, legends, grids, and boundaries using direct plt. commands:

3. Analyzing Numerical Data

Numerical data is continuous and is best visualized using plots that show trends, distributions, or correlations. Line plots are used to show how numerical values change over a continuous interval (typically time).

Histograms (Distributions)

Histograms show the frequency distribution of a continuous numerical variable by grouping values into “bins”.

Scatter Plots (Correlation)

Scatter plots show the relationship (correlation) between two numerical variables.

4. Analyzing Categorical Data

Categorical data represents discrete groups (like gender, courses, or jobs) and is best visualized using bar charts.

Vertical Bar Charts

Used to compare numerical values across different categorical groups.

Horizontal Bar Charts (barh)

Highly useful when category names are long, preventing text overlap on the X-axis.

5. Analyzing Numerical vs. Categorical Data

To compare the distribution of a numerical variable across different categories, we use Box Plots.

Box Plots (Whisker Plots)

A box plot summarizes a dataset using five statistics: minimum, first quartile (Q1), median, third quartile (Q3), and maximum. It is also excellent for identifying outliers.

6. Multi-Plots and Subplots

To display multiple plots side-by-side or stacked in a grid, we transition to the Object-Oriented Interface using plt.subplots(rows, columns).

Grid Layouts with Subplots


Practice and Next Steps

Before moving to the next section, make sure to practice your Matplotlib skills using the interactive notebook:

Matplotlib Practice Exercise

Practice your skills using the interactive notebook.💻 VS Code | 🚀 Colab | 📥 Download

Next Library: Seaborn

Learn how to create beautiful, advanced statistical charts with Seaborn.