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

## 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.

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.

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.

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:

Fig: subplot()

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

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:

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:

Fig: Histogram

### 3. Scatter Plots

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

Fig: Scatter plot

Output:

Fig: Scatter plot

## Matplotlib Three-Dimensional Plotting

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

Example:

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:

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!