An Introduction to Matplotlib for Beginners

To make necessary statistical inferences, it becomes essential to visualize your data, and Matplotlib is one such solution for Python users. It is a comprehensive plotting library useful for those working with Python and NumPy. In this Matplotlib tutorial, we will cover all the functionality of this module, including the following topics:

  • What is Matplotlib?
  • Getting started with pyplot
  • Matplotlib: subplots
  • Important types of plots
    • Bar charts
    • Histograms
    • Scatter plots
  • Matplotlib: three-dimensional plotting
  • Hands-on demo

What is Matplotlib?

  • Matplotlib is an open-source drawing library that supports various drawing types
  • You can generate plots, histograms, bar charts, and other types of charts with just a few lines of code
  • It’s often used in web application servers, shells, and Python scripts

Python Training Course

Learn Data Operations in PythonExplore Course
Python Training Course

Getting Started With Pyplot

Pyplot is a Matplotlib module that provides simple functions for adding plot elements, such as lines, images, text, etc. to the axes in the current figure.

Let's begin our tutorial with a simple graph that uses fundamental Matplotlib code in Jupyter Notebook. 

basic-plot.

 Fig: Basic plot-matplotlib tutorial

Note that the first array appears on the x-axis, and the second array appears on the y-axis of the plot.

Let us now see how we can add a title, as well as the x-axis and y-axis names, using the title(), xlabel(), and ylabel() methods, respectively.

label.

Fig: label()

Users can also specify the size of the figure using the figure() method. Additionally, users can pass values as tuples, which make up the length of rows and columns to the argument figsize.

figure

Fig: figure() method-matplotlib tutorial

Matplotlib Subplots

You can use the subplot() method to add more than one plot in a figure. 

Syntax: plt.subplots(nrows, ncols, index)

The three-integer arguments specify the number of rows and columns and the index of the subplot grid.

Example:

subplot

Fig: subplot()

The add_subplot() function of the figure class enables us to add a graph inside a graph.

add

Fig: add_subplot()

Important Types of Plots

  • Bar graphs
  • Histograms
  • Scatter plots

1. Bar Graphs

A  bar graph presents data with heights and lengths proportional to the values they present.

Syntax: ax.bar(x, height, width, bottom, align)

Example:

bar-graph

Fig: Bar graph-matplotlib tutorial

The axes label is a region of the image with data space. 

An axes object is added to figure by the add_axes() method. The syntax for the axes object is: ax = fig.add_axes([left,right,width,height]).

2. Histograms

A histogram is used to understand the distribution of a continuous numerical variable.

Example:

histogram

Fig: Histogram

3. Scatter Plots

Scatter plots are used to represent values for two different numeric variables.

scatter

Fig: Scatter plot

Output:

scatter2.

Fig: Scatter plot

Matplotlib Three-Dimensional Plotting

Importing the mplot3d toolkit enables users to create three-dimensional plots. 

Example:

/3d

Fig: Three-dimensional plotting-matplotlib tutorial

Practice Example

For this practice drill, you’ll write a Python program that will create a pie chart showing the popularity of programming languages.

Solution:

soplution

Fig: Solution

Autopct is used to display values on the pie slice.

Looking forward to make a move to programming? Take up the Python Training Course and begin your career as a professional Python programmer.

Conclusion

In this Matplotlib tutorial article, we discussed the basic concepts related to the Matplot library. We looked at how to create basic graphs and plots, and its different functions. We discussed three-dimensional plotting and sub-plots as well. 

If you have any other questions, please feel free to ask them in our comments section, and we'll get back to you as soon as possible.

Want to Learn More About Python?

Python is one of the most widely used programming languages in the fields of data science and analytics. That means there’s a high demand for qualified professionals. If you’re ready to boost your career in these exciting fields, check out our Python Training Course today!

About the Author

Aryan GuptaAryan Gupta

Aryan is a tech enthusiast who likes to stay updated about trending technologies of today. He is passionate about all things technology, a keen researcher, and writes to inspire. Aside from technology, he is an active football player and a keen enthusiast of the game.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.