What is Matplotlib?

Matplotlib is a Python library for 2D plotting. It is used to create static, animated, and interactive visualizations in Python. Matplotlib makes easy things easy and hard things possible. Create publication quality plots. Make interactive figures that can zoom, pan, update.

Matplotlib is a cross-platform, data visualization and graphical plotting library (histograms, scatter plots, bar charts, etc) for Python and its numerical extension NumPy. As such, it offers a viable open source alternative to MATLAB.

Matplotlib was created by John D. Hunter. Matplotlib is open source and we can use it freely. Matplotlib is mostly written in python, a few segments are written in C, Objective-C and Javascript for Platform compatibility.

Here are some of the features of Matplotlib:

  • It can create a variety of plots, including histograms, scatter plots, bar charts, pie charts, and more.
  • It is highly customizable, allowing you to change the appearance of your plots to suit your needs.
  • It is easy to use, with a simple and intuitive syntax.
  • It is well-documented, with a comprehensive user manual and many online tutorials.

If you are looking for a powerful and versatile data visualization library for Python, Matplotlib is a great option.

Installation of Matplotlib

If you have Python and PIP already installed on a system, then installation of Matplotlib is very easy.

Install it using this command:

C:\\Users\\Your Name>pip install matplotlib

If this command fails, then use a python distribution that already has Matplotlib installed, like Anaconda, Spyder etc.

Import Matplotlib

Once Matplotlib is installed, import it in your applications by adding the import module statement:

import matplotlib

Checking Matplotlib Version

The version string is stored under __version__ attribute.

import matplotlib
print (matplotlib. __version__)

Matplotlib Pyplot

matplotlib.pyplot is a Python library that provides a state-based interface to matplotlib, a data visualization library.

Matplotlib Plotting

The plot() function is used to draw points (markers) in a diagram.
By default, the plot() function draws a line from point to point.
The function takes parameters for specifying points in the diagram.

Matplotlib Markers

You can use the keyword argument marker to emphasize each point with a specified marker.

Matplotlib Line

You can use the keyword argument Linestyle, or shorter ls, to change the style of the plotted line.

Matplotlib Labels and Title

With Pyplot, you can use the xlabel() and ylabel() functions to set a label for the x- and y-axis.

Matplotlib Adding Grid Lines

With Pyplot, you can use the grid() function to add grid lines to the plot.

Matplotlib Subplot

With the subplot() function you can draw multiple plots in one figure:

Matplotlib Scatter

With Pyplot, you can use the scatter() function to draw a scatter plot.
The scatter() function plots one dot for each observation. It needs two arrays of the same length, one for the values of the x-axis, and one for values on the y-axis.

Matplotlib Bars

With Pyplot, you can use the bar() function to draw bar graphs:

Matplotlib Histograms

A histogram is a graph showing frequency distributions.
It is a graph showing the number of observations within each given interval.

Matplotlib Pie Charts

With Pyplot, you can use the pie() function to draw pie charts: