Python3 Matplotlib Example Mastering Data Visualization With Code Snippets

Logan Jul 13, 2026

Python is renowned for its extensive library support, offering countless tools to perform complex tasks with remarkable ease. Matplotlib, Python's graphical plotting module, is one such gem which simplifies the process of creating static, animated, and interactive visualizations in Python. It's often the go-to library when dealing with data visualization. Let's explore some practical examples of how to use Matplotlib in Python3.

9 ways to set colors in Matplotlib
9 ways to set colors in Matplotlib

Matplotlib provides a MATLAB-like syntax, making it user-friendly and a great starting point for beginners. It supports a wide array of different file formats so you can save your plots in varied formats too. Let's dive into its features and functionalities through some hands-on examples.

Top 50 matplotlib Visualizations - The Master Plots (w/ Full Python Code) | ML+
Top 50 matplotlib Visualizations - The Master Plots (w/ Full Python Code) | ML+

Matplotlib Installation and Basic Usage

Before we begin, make sure you have Python3 installed. If Matplotlib is not already installed, you can add it using pip, Python's package manager:

a poster with graphs and diagrams on it's back cover, which includes information about the various types of data
a poster with graphs and diagrams on it's back cover, which includes information about the various types of data

pip install matplotlib

After installation, you can import it in your script with:

import matplotlib.pyplot as plt

Let's start by plotting a simple line graph. Here's how you do it:

matplotlib
matplotlib

Plotting a Line Graph

Matplotlib provides two methods for line plotting; `plot()` and `plot()` with x and y parameter separation.

x = [1, 2, 3, 4, 5] y = [1, 4, 9, 16, 25] plt.plot(x, y) plt.show()

This will plot y = x² for values from 1 to 5.

Matplotlib Cheat Sheet for Data Analysts (Chapter 6/15) | Python for Data Analysts
Matplotlib Cheat Sheet for Data Analysts (Chapter 6/15) | Python for Data Analysts

You can make your plots more readable and visually appealing by adding labels, title, and legends. Here's how to do it:

plt.plot(x, y, 'o-', label='y = x^2') plt.xlabel('x') plt.ylabel('y') plt.title('Line Plot Example') plt.legend() plt.show()

Bar Charts with Matplotlib

Equally, Matplotlib makes bar chart creation simple. You can use `bar()` function for this:

Matplotlib Visualization with Python
Matplotlib Visualization with Python

xl = ['P', 'Q', 'R', 'S'] yl = [3, 8, 1, 10] plt.bar(xl, yl) plt.show()

For better readability, you can adjust the bar width using the `width` parameter and rotatelabels:

plt.bar(xl, yl, width = 0.2, align='center') plt.xticks(xl, xl, rotation=45) plt.show()

Advanced Matplotlib Features

Data Visualization using Matplotlib
Data Visualization using Matplotlib
Day 28: Matplotlib in Python 📊
Day 28: Matplotlib in Python 📊
scatter plot using matplotib in python
scatter plot using matplotib in python
How to plot a graph in python using matplotlib?
How to plot a graph in python using matplotlib?
Matplotlib - Introduction to Python Plots with Examples | ML+
Matplotlib - Introduction to Python Plots with Examples | ML+
Intro to Python Packages: Numpy and Matplotlib
Intro to Python Packages: Numpy and Matplotlib
8 Matplotlib Visualizations Every Python Dev Should Master Python Matplotlib Code And Graph, Matplotlib Python, Python, Coding, Instagram
8 Matplotlib Visualizations Every Python Dev Should Master Python Matplotlib Code And Graph, Matplotlib Python, Python, Coding, Instagram
A Beginner’s Guide to matplotlib for Data Visualization and Exploration in Python
A Beginner’s Guide to matplotlib for Data Visualization and Exploration in Python
a screenshot of the matplotib line chart for windows and macs
a screenshot of the matplotib line chart for windows and macs
Matplotlib Tutorial
Matplotlib Tutorial
Jupyter Notebook Viewer
Jupyter Notebook Viewer
Display a 3D surface in Python with Matplotlib  #python #Matplotlib #code #fblifestyle Step By Step Instructions, Web Design, Coding, Python, Software
Display a 3D surface in Python with Matplotlib #python #Matplotlib #code #fblifestyle Step By Step Instructions, Web Design, Coding, Python, Software
Program to Plot the absolute function using Matplotlib in Python  Programming
Program to Plot the absolute function using Matplotlib in Python Programming
How to Plot a Histogram in Python using Matplotlib
How to Plot a Histogram in Python using Matplotlib
matplotlib.pyplot Cheat Sheet
matplotlib.pyplot Cheat Sheet
How to Use Colormaps with Matplotlib to Create Colorful Plots in Python
How to Use Colormaps with Matplotlib to Create Colorful Plots in Python
5 Matplotlib Hacks for Better Python Charts
5 Matplotlib Hacks for Better Python Charts
Matplotlib Cheat Sheet: Plotting in Python
Matplotlib Cheat Sheet: Plotting in Python
a screenshot of a computer screen with the text installing matplotib
a screenshot of a computer screen with the text installing matplotib
a screenshot of a computer screen with an image of a graph on it and the text multiple subjects using matplot
a screenshot of a computer screen with an image of a graph on it and the text multiple subjects using matplot

Matplotlib also supports 3D plotting and subplots, allowing complex visualizations. Let's explore these briefly.

3D Plotting with Matplotlib

Matplotlib's `mplot3d` toolkit enables you to create 3D plots. For instance, you can create a 3D scatter plot like this:

from mpl_toolkits.mplot3d import Axes3D fig = plt.figure() ax = fig.add_subplot(111, projection='3d') x = [1, 2, 3, 4, 5] y = [2, 3, 4, 5, 6] z = [3, 4, 5, 6, 7] ax.scatter(x, y, z) plt.show()

Subplots and Grids

You can plot multiple graphs on a single figure using subplots and grids. Matplotlib offers `subplots()` function for this. Here's a simple example:

x = [1, 2, 3, 4, 5] y = [1, 4, 9, 16, 25] fig, (ax1, ax2) = plt.subplots(1, 2) ax1.plot(x, y) ax2.bar(x, y) plt.show() Finally, remember to always test and customize your plots. There are numerous more customization options Matplotlib provides, so keeping practicing and playing around with it. Now, go ahead and find a dataset that interests you, and visualize it using Matplotlib to gain valuable insights from your data.