Matplotlib Tutorial (original) (raw)
Last Updated : 17 Mar, 2025
Matplotlib is an open-source visualization library for the Python programming language, widely used for creating **static, **animated and **interactive plots. It provides an **object-oriented API for embedding plots into applications using general-purpose GUI toolkits like Tkinter, Qt, GTK and wxPython. It offers a variety of plotting functionalities, including line plots, bar charts, histograms, scatter plots and 3D visualizations. Created by John D. Hunter in 2003, Matplotlib has become a fundamental tool for data visualization in Python, extensively used by data scientists, researchers and engineers worldwide.
To learn Matplotlib step-by-step, refer to our page: Matplotlib Step-by-Step Guide.
**Important Facts to know:
- **Matplotlib Pyplot: The pyplot module is a collection of functions that make Matplotlib work like MATLAB, providing a simple interface for creating plots.
- **Figure and Axes: In Matplotlib, figures represent the overall container, while axes refer to the individual plots within a figure.
- **Integration with Pandas: Matplotlib works seamlessly with Pandas DataFrames, enabling efficient data visualization.
What is Matplotlib in Python used for?
With Matplotlib, we can perform a wide range of visualization tasks, including:
- Creating basic plots such as line, bar and scatter plots.
- Customizing plots with labels, titles, legends and color schemes.
- Adjusting figure size, layout and aspect ratios.
- Saving plots in various formats like PNG, PDF and SVG.
- Combining multiple plots into subplots for better data representation.
- Creating interactive plots using the widget module.
Learn Matplotlib
Now that we know what Matplotlib is and its uses, let’s move towards the tutorial part. Below, you will find sections ranging from basic to advanced topics that will help you master Matplotlib.
Matplotlib Basics
In this section, we will explore the fundamentals of Matplotlib. We will start with an introduction, learn how to install it and understand its core functionalities. Additionally, we will cover how to use Jupyter Notebook for interactive visualizations.
Plotting with Matplotlib
This section focuses on different types of plots and their implementations using Matplotlib.
- Basic graph in Matplotlib
- Line Plots in Matplotlib
- Bar Charts with Matplotlib
- Scatter Plots in Matplotlib
- Histograms in Matplotlib
- Pie Charts with Matplotlib
- 3D plot in Matplotlib
Customization in Matplotlib
Matplotlib provides extensive customization options for better visualization and aesthetics.
- Markers in Matplotlib
- Adding Labels in Matplotlib
- Configuring Grid in Matplotlib
- Creating Subplots in Matplotlib
- Styling Plots with Matplotlib
- Resize a Plot in Matplotlib
- Adjust Plot Transparency in Matplotlib
- Change Fonts in Matplotlib
- Set Tick Label Font Size in Matplotlib
- Change Plot Background Color in Matplotlib
- Move Axis Labels in Matplotlib
- Hide Axis, Borders and Extra Space in Matplotlib
Advanced Plotting with Matplotlib
Explore advanced visualization techniques using Matplotlib’s powerful functionalities.
- Creating 3D Plots
- Surface and Wireframe Plots
- Heatmaps and Contour Plots
- Animating Plots with Matplotlib
Saving and Exporting Plots
Save your visualizations in various formats for reports and presentations.
Toolkits in Matplotlib
Several toolkits extend Matplotlib’s functionality, some of which are external downloads, while others are included with Matplotlib but have external dependencies. Here are some of the most notable toolkits:
- **Seaborn: A high-level statistical data visualization library built on top of Matplotlib, extremely popular for creating attractive and informative statistical graphics with minimal code.
- **Mplot3d: Integrated into Matplotlib itself, this toolkit is the go‑to choice for creating 3‑D plots with ease and flexibility.
- **GeoPandas: A library that leverages Matplotlib for geospatial plotting, simplifying the handling of geospatial data without needing a spatial database.
- **Cartopy: A modern mapping library offering an object‑oriented approach to map projections and geospatial data, largely replacing Basemap in new projects.
- **Tikzplotlib: A niche toolkit that converts Matplotlib figures into LaTeX-friendly TikZ/PGFPlots code, ideal for producing high-quality, publication-ready plots.
Matplotlib with Pandas and Seaborn
Integrate Matplotlib with Pandas and Seaborn for enhanced data visualization.
- Using Matplotlib with Pandas DataFrames
- Visualizing Time Series Data
- Combining Seaborn and Matplotlib
- Creating Pair Plots
- Correlation Heatmaps
Matplotlib Quiz
Test your knowledge of Matplotlib with this quiz. It covers essential topics such as plotting techniques, customization and integration with other libraries.
Projects
- Create a 3D Sine Wave with Matplotlib in Python
- Visualize Bubble Sort with Python
- Merge Sort Visualization Using Matplotlib
- Quick Sort Visualization Using Matplotlib
- 3D Quick Sort Visualization with Matplotlib in Python
- 3D Merge Sort Visualization with Matplotlib
- 3D Insertion Sort Visualization with Matplotlib in Python
- Create a Time Series Plot with Rolling Average in Python
- Plot Different Sounds Using Python and Matplotlib
- Visualizing COVID-19 Data with Matplotlib in Python
- Analyzing Used Car Prices Using Python