Introduction to Matplotlib in Python

Introduction to Matplotlib in Python
Tutorial Playlist

Handling data is a crucial skill in today’s world.  As technology has grown, there is a massive amount of data that is generated and consumed every day. Thus, handling this data in a rather effective manner becomes the main goal of Data Scientists. Python is one of the most popular programming languages for handling data, and it provides various libraries to make this process easier. We can make use of Python to deal with data sets that require operations, like calculating statistics, sales, marketing, plotting on graphical platforms, etc., to be done on them. In this blog, you will learn about matplotlib along with different types of charts that can be generated using Matplotlib in Python in detail with examples.

Table of Contents:

What is Matplotlib in Python?

Matplotlib is a very well-known plotting library for Python that provides a relatively interactive interface for plotting various graphs and charts. It helps in data visualization and makes it easier to analyze trends and patterns. With Matplotlib, we can plot line graphs, bar charts, histograms, scatter plots, and so on. Data scientists, machine learning experts, and scientists largely make use of it. Multiple GUI toolkits are supported by Matplotlib including wxPython, Tkinter, and Qt, letting you embed plots into applications. Its original developer was John D. Hunter, but later developed by Michael Droettboom. Matplotlib forms the core of SciPy

In simple words, Matplotlib helps turn raw data into visual insights, making complex data and information easier to analyze and understand.

Watch this Matplotlib Python Tutorial for Beginners:

Video Thumbnail

Why Should You Use Matplotlib in Python?

Matplotlib, a Python plotting library, allows users to create a variety of different types of plots and graphs and is designed to provide visualization for data in a manner that helps understand the way the data behaves, trends, and patterns. It supports various types of charts, including line and bar plots, scatter, and pie charts by integrating with libraries like NumPy and Pandas. It is effectively used in data science, machine learning, and scientific research.

The following are some main features of Matplotlib:

  • Easy to Use: Simpler code is used that allows a fast setup for your data plots.
  • Multiple Plot Types: Generate a variety of visualizations: line plots, bar charts, histograms, scatter plots, etc.
  • Highly Customizable: Can easily change the colors, the title and axis label, and the style of your choice.
  • Works with NumPy and Pandas: Friendly interfacing with these popular data analysis libraries.
  • Supports Interactive and 3D Plots: Develop data visualizations that help you interactively perceive data and allow you to build three-dimensional plots.

Installation of Matplotlib in Python

Before using Matplotlib in Python, it is important to know how to install it. There are two common ways to install Matplotlib in Python:

Installing Matplotlib in Anaconda PowerShell

Matplotlib can be installed in Anaconda using conda-forge, which provides updated and well-maintained packages. This method ensures compatibility within the Anaconda environment.

“conda install  matplotlib”

Once Matplotlib is installed in the Anaconda Prompt, the following message is displayed

To verify whether Matplotlib is successfully installed, you can check the version of Matplotlib in Anaconda Prompt using

python -c “import matplotlib; print(matplotlib.__version__)”

The above output displays the version of Matplotlib, which verifies the successful installation of Matplotlib.

Installing Matplotlib using PIP

If you are not using Anaconda, Matplotlib can be installed directly using PIP, Python’s default package manager. This works well for standard Python installations and virtual environments.
You can install Matplotlib using pip with the following command:

“pip install matplotlib”

This message is displayed when Matplotlib is successfully imported in Python.

Verifying Successful Installation of Matplotlib in Python

To check if Matplotlib is installed correctly, we can import it and print its version. If the version number appears, it means Matplotlib is successfully installed.

Check the successful installation in the IDE

Python

Output:

Explanation: Here, the version of Matplotlib is displayed, confirming that Matplotlib is successfully installed in Python

Check the successful installation in Command Prompt

python -c “import matplotlib; print(matplotlib.__version__)”

Output:

Explanation: Here, the version of Matplotlib is displayed, confirming that Matplotlib is successfully installed in Python.

Importing Matplotlib in Python with a Basic Example

Importing matplotlib.pyplot as pltPyplot is used for plot or figure manipulation. Matplotlib.pyplot enables Python Matplotlib to operate just like MATLAB. Let’s see how to import matplotlib in Python.

Python Matplotlib Example:

Here, let’s plot a basic graph using Matplotlib in Python

Example:
Python
Output:

Explanation: The graph can be used to plot three straight lines. We make this possible by using the plotting library in Matplotlib.

Basic Plotting with Matplotlib in Python

Plotting is the visual representation of data as a graph or chart. It helps learn the pattern, trend, and relationship contained in the data. By using different types of plots, we can present data more effectively for analysis and decision-making. Matplotlib helps in plotting effective graphs or charts to visualize the data.

1. Creating Scatter Plots Using Matplotlib in Python

A scatter plot is used to display individual data points to show relationships between two numerical variables. They are useful for identifying correlations or clusters in data. The scatter plots can be effectively created using Matplotlib.

Example:

Python

Output:

Explanation: This scatter plot represents the ratings of different Intellipaat courses. The X-axis shows the course names, while the Y-axis displays their ratings out of 5.

2. Adding Axis Labels to the Plot Using Matplotlib in Python

Axis labels provide important information about the data being displayed in the plot. The axis labels describe what each axis represents, making the graph easier to understand.

Example:

Python

Output:

Explanation: This plot shows the number of enrollments for different “Intellipaat Courses”. The labels help identify the meaning of each axis—courses on the X-axis and enrollments on the Y-axis.

Common Terminologies Used in Matplotlib with Python

Plot

It is an illustration that can be represented using a graph.

Example:

Python

Output:

Explanation: Here, when we take the plot parameters as [1,1], we get the above plot as the output.

Get 100% Hike!

Master Most in Demand Skills Now!

Figure

It is a diagram or a shape that can be formed by a collection of plots in different dimensions.

Example:

Python

Output:

 

Explanation: Figure 1 helps print the first graph with the plot([1,1]), and Figure 2 helps print the second graph with the plot([1,2]).

Label

It is used to add labels or names to the respective x and y axes.

Example:

Python

Output:

Explanation: In the above graph, the horizontal axis is labeled as ‘Active Users’ and the vertical axis is labeled as ‘Technologies’, which makes the graph simpler to understand.

Title

It is used to display the title of the graph using title().

Example:

Python

Output:

Explanation:
In the above graph, the horizontal axis is labeled as ‘X-axis’ and the vertical axis is labeled as ‘Y-axis’, and the title is displayed as ‘GRID REPRESENTATION’.

Grid

It is a collection of objects and functions that is concerned with 3-dimensional data using grid().

Example:

Python

Output:

Explanation:
A grid-based representation is displayed in the above output, and it helps locate specific regions in the graph.

Subplot

A subplot() function can be called to plot multiple plots in the same figure.

Example:

Python

Output:

Explanation:
The above representation explains how subplots are obtained and how two subplots are plotted in the same figure.

Creating Multiple Plots Using Matplotlib in Python

We can use the subplot() function to create multiple plots in a single figure. This helps in comparing different datasets side by side. It makes the data easier to understand and analyze. We can arrange the plots in rows and columns as needed.

Example:

Python

Output:

Explanation:
The plt.subplot() function creates multiple plots in one figure. The first plot shows enrollments, and the second plot shows course ratings.

Plot Manipulation Description

  • Plot creation: This depends on the type of module that can be used in Python. Creating a plot is the key aspect of plotting, where we decide the plot upon which a figure is constructed. Figure and axes initialization is also carried out under plot creation.
  • Plot routines: Visualization techniques of viewing data from the simplest to the advanced forms are part of plot routines
  • Plot customization: This includes adding plot titles, legends, axes labels, layouts, etc.
  • Manipulation of plots also includes saving of plots, clearing the content, displaying figures, clearing axes, etc.
  • Images, colors, and text are some of the best features that can be included within the plot.

Matplotlib Python Plotting Ways

Various plotting techniques or ways can be carried out on the data provided, and some of these plotting types are as follows:

Here, let us consider a survey that has been conducted to track enrollments in different courses over five days in Intellipaat. The collected data can be visualized using different plotting methods. Let’s consider the data below for generating different plots in Matplotlib.

COURSES

DAYS

TOTAL ENROLLMENTS

DATA SCIENCE

AI & ML

CYBER SECURITY

WEB DEVELOPMENT

DAY 1 5000 8000 7000 8000
DAY 2 4000 2000 2000 2000
DAY 3 7000 2000 6000 2000
DAY 4 8000 5000 4000 5000
DAY 5 2000 6000 6000 6000

1. Line Plot Using Matplotlib in Python

A line plot is a graphical representation of data where points are plotted and connected by lines to show the changes over a period of time.

Example:

Python

Output:

Explanation:
The above graph represents course enrollments over five days using different colored lines. Each line corresponds to a different course, showcasing trends in enrollment numbers.

2. Bar Chart Plot Using Matplotlib in Python

A bar chart is a graphical representation that uses rectangular bars to display data based on categories. It is useful for comparing different groups. Here, let’s see how the bar chart plot can be generated using Matplotlib

Example:
Python

Output:
Explanation:
The bar chart above represents enrollments in different courses over five days. Each course is represented by a different color, making it easy to compare enrollment trends.

3. Area_Plot_Using_Matplotlib_in_Python

An area plot is used to visualize large data. It is similar to a line plot, but the area between the line and the axis is filled with colors to represent different categories to make it simpler to understand the data.

Example:

Python

Output:

Explanation:
The above area plot represents the enrollments for different courses over five days. The shaded part shows the trends in enrollments, with each course represented by a different colour.

4. Pie Plot Using Matplotlib in Python

In a pie plot, statistical data can be represented in a circular graph where the circle is divided into portions that denote a particular data, where each portion is called a slice.

Example:

Python

Output:

Explanation:
The above pie chart represents the total enrollments for each course over five days. Each slice shows the proportion of enrollments for a particular course, making it easy to compare the popularity of the courses

5. 3D Plot Using Matplotlib in Python

Plotting of data along x, y, and z axes to enhance the display of data represents the 3-dimensional plotting. 3D plotting is an advanced plotting technique that gives us a better view of the data representation along the three axes of the graph.

Example:

Python

Output:

Explanation:
The above 3D plot shows the number of enrollments for different courses over five days. Each line represents a course, plotted in a 3D space to enhance visualization.

Syntax for plotting 3D graphs:

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection=’3d’)

The import Axes3D is mainly used to create an axis by making use of the projection=3d keyword. This enables a 3-dimensional view of any data that can be written along with the above-mentioned code.

6. Histogram Plot Using Matplotlib in Python

The plotting of numerical data in a precise manner by using rectangular blocks forms the basis of histogram plotting. A probability distribution can be estimated using a histogram plot.

Example:

Python

Output:


Explanation:
The above histogram represents the number of enrollments for various courses. It shows how many times different enrollment values appear within ranges.

Python(Matplotlib) vs MATLAB

Python Programming MATLAB
It is an open-source programming language, free to use. MATLAB is a commercial platform. Hence, it is not free.
Matplotlib is more flexible and capable of plotting. Plotting is comparatively not as flexible and capable as Python plotting.
Python provides a large number of libraries to work with. It is tricky to add libraries and work with them in MATLAB.
Python is an easy-to-read and powerful programming language. MATLAB is not as powerful as Python.
Matplotlib plots faster and more efficiently in Python. Plotting data in MATLAB requires time and effort.
An integrated development environment (IDE) needs to be added, additionally. IDE will be provided within the MATLAB environment.
Code can be used in multiple systems. It is portable. Code portability is restricted.
Python supports namespaces to organize code better. The core of MATLAB does not support namespaces.

Conclusion

With this, we come to the end of this tutorial. In this Python tutorial, we learned the installation process of Matplotlib in detail and explored different types of plotting techniques using Matplotlib, from basic line plots to advanced 3D plots. We learned how to visually represent data effectively, making it easier to analyze trends and patterns to implement various types of plotting techniques.  Dealing with multiple or huge amounts of data and representing them in graphs for better understanding are the major uses of Matplotlib in Python. If you want to understand why Python is the top choice for Data Science, explore our in-depth Python Data Science tutorial.

To take your skills to the next level, enroll in our Python training course and gain hands-on experience. Also, prepare for job interviews with our Python developer interview questions, prepared by industry experts.

Our Python Courses Duration and Fees

Program Name
Start Date
Fees
Cohort Starts on: 12th Apr 2025
₹20,007

About the Author

Technical Research Analyst - Full Stack Development

Kislay is a Technical Research Analyst and Full Stack Developer with expertise in crafting Mobile applications from inception to deployment. Proficient in Android development, IOS development, HTML, CSS, JavaScript, React, Angular, MySQL, and MongoDB, he’s committed to enhancing user experiences through intuitive websites and advanced mobile applications.