**What Is Python Matplotlib?**

Matplotlib is a plotting library for the Python programming language and its numerical mathematics extension NumPy. It provides an object-oriented API for embedding plots into applications using general-purpose GUI toolkits like Tkinter, wxPython, Qt, or GTK+.

Pyplot is a Matplotlib module which provides a MATLAB-like interface. Matplotlib is designed to be as usable as MATLAB, with the ability to use Python and the advantage of being free and open-source. matplotlib.pyplot is a plotting library used for 2D graphics in the python programming language. It can be used in python scripts, shell, web application servers, and other graphical user interface toolkits.

There are several toolkits that are available that extend python Matplotlib functionality.

- Basemap: It is a map plotting toolkit with various map projections, coastlines, and political boundaries.
- Cartopy: It is a mapping library featuring object-oriented map projection definitions, and arbitrary point, line, polygon and image transformation capabilities.
- Excel tools: Matplotlib provides utilities for exchanging data with Microsoft Excel.

Mplot3d: It is used for 3-D plots. - Natgrid: It is an interface to the “natgrid” library for irregular gridding of the spaced data.
- GTK tools: mpl_toolkits.gtktools provides some utilities for working with GTK. This toolkit ships with matplotlib, but requires pygtk.
- Qt interface
- Mplot3d: The mplot3d toolkit adds simple 3D plotting capabilities to matplotlib by supplying an axes object that can create a 2D projection of a 3D scene.
- matplotlib2tikz: export to Pgfplots for smooth integration into LaTeX documents.

**Types of Plots**

There are various plots which can be created using python Matplotlib. Some of them are listed below:

- Bar Graph
- Histogram
- Scatter Plot
- Line Plot
- 3D plot
- Area Plot
- Pie Plot
- Image Plot

We will demonstrate some of them in detail.

But before that, let me show you elementary codes in python matplotlib in order to generate a simple graph.

from matplotlib import pyplot as plt #Plotting to our canvas plt.plot([5,6,7],[8,9,5]) #Showing what we plotted plt.show()

So, with three lines of code, you can generate a basic graph using python matplotlib.

Let us see how can we add title, labels to our graph created by python matplotlib library to bring in more meaning to it. Consider the below example:

from matplotlib import pyplot as plt plt.plot([5,2,7],[2,16,4]) plt.title('Info') plt.ylabel('Y axis') plt.xlabel('X axis') plt.show()

You can even try many styling techniques to create a better graph by changing the width or color of a particular line or what if you want to have some grid lines, there you need styling!

The style package adds support for easy-to-switch plotting “styles” with the same parameters as a matplotlibrc file.

There are a number of pre-defined styles provided by matplotlib. For example, there’s a pre-defined style called “ggplot”, which emulates the aesthetics of ggplot (a popular plotting package for R). To use this style, just add:

import matplotlib.pyplot as plt plt.style.use('ggplot')

To list all available styles, use:

print(plt.style.available) ---------------------------- o/p: ['seaborn-darkgrid', 'Solarize_Light2', 'seaborn-notebook', 'classic', 'seaborn-ticks', 'grayscale', 'bmh', 'seaborn-talk', 'dark_background', ' ggplot', 'fivethirtyeight', '_classic_test', 'seaborn-colorblind', 'seaborn-deep', 'seaborn-whitegrid', 'seaborn-bright', 'seaborn-poster', 'sea born-muted', 'seaborn-paper', 'seaborn-white', 'fast', 'seaborn-pastel', 'seaborn-dark', 'tableau-colorblind10', 'seaborn', 'seaborn-dark-palett e']

So, let me show you how to add style to a graph using python matplotlib. First, you need to import the style package from python matplotlib library and then use styling functions as shown in below code:

from matplotlib import pyplot as plt from matplotlib import style style.use(('dark_background')) x = [5,8,10] y = [12,16,6] x2 = [6,9,11] y2 = [6,15,7] plt.plot(x,y,'r-o',label='line one', linewidth=5) plt.plot(x2,y2,'c',label='line two',linewidth=5) plt.title('Epic Info') plt.ylabel('Y axis') plt.xlabel('X axis') plt.legend() plt.grid(True,color='w') plt.show()

import numpy as np import matplotlib.pyplot as plt with plt.style.context(('dark_background')): plt.plot(np.sin(np.linspace(0, 2 * np.pi)), 'r-o') # Some plotting code with the default style plt.show()

Now, we will understand the different kinds of plots. Let’s start with the bar graph!

**Matplotlib: Bar Graph**

A bar graph uses bars to compare data among different categories. It is well suited when you want to measure the changes over a period of time. It can be plotted vertically or horizontally. Also, the vital thing to keep in mind is that longer the bar, the greater is the value. Now, let us practically implement it using python matplotlib.

from matplotlib import pyplot as plt style.use('dark_background') plt.bar([1,2,3,4,5],[8,7,5,6,4], label="BMW", color='b', width=0.2) plt.bar([1.2,2.2,3.2,4.2,5.2],[2,3,5,10,8], label="Audi", color='r',width=0.2) plt.legend() plt.xlabel('Days') plt.ylabel('Distance (kms)') plt.title('Bar Plot') plt.show()

When I run this code, it generates a figure like below:

In the above plot, I have displayed a comparison between the distance covered by two cars BMW and Audi over a period of 5 days. Next, let us move on to another kind of plot using python matplotlib – Histogram

**Matplotlib – Histogram**

Let me first tell you the difference between a bar graph and a histogram. Histograms are used to show a graphical representation of the distribution of numerical data whereas a bar chart is used to compare different entities.

It is an estimate of the probability distribution of a continuous variable (quantitative variable) and was first introduced by Karl Pearson. It is a kind of bar graph.

To construct a histogram, the first step is to “bin” the range of values — that is, divide the entire range of values into a series of intervals — and then count how many values fall into each interval. The bins are usually specified as consecutive, non-overlapping intervals of a variable. The bins (intervals) must be adjacent and are often (but are not required to be) of equal size.

Basically, histograms are used to represent data given in the form of some groups or we can say when you have arrays or a very long list. X-axis is about bin ranges where Y-axis talks about frequency. So, if you want to represent the age-wise population in form of the graph then histogram suits well as it tells you how many exist in certain group range or bin if you talk in the context of histograms.

In the below code, I have created the bins in the interval of 10 which means the first bin contains elements from 0 to 9, then 10 to 19 and so on.

import matplotlib.pyplot as plt population_age = [22,55,62,45,21,22,34,42,42,4,2,102,95,85,55,110,120,70,65,55,111,115,80,75,65,54,44,43,42,48] bins = [0,10,20,30,40,50,60,70,80,90,100] plt.hist(population_age, bins, rwidth=0.8) plt.xlabel('age groups') plt.ylabel('Number of people') plt.title('Histogram') plt.show()

When I run this code, it generates a figure like below:

As you can see in the above plot, Y-axis tells about the age groups that appear with respect to the bins. Our biggest age group is between 40 and 50.

**Matplotlib: Scatter Plot**

A scatter plot is a type of plot that shows the data as a collection of points. The position of a point depends on its two-dimensional value, where each value is a position on either the horizontal or vertical dimension. Usually, we need scatter plots in order to compare variables, for example, how much one variable is affected by another variable to build a relation out of it.

Consider the below example:

import matplotlib.pyplot as plt x = [1,1.5,2,2.5,3,3.5,3.6] y = [7.5,8,8.5,9,9.5,10,10.5] x1=[8,8.5,9,9.5,10,10.5,11] y1=[3,3.5,3.7,4,4.5,5,5.2] plt.scatter(x,y, label='high income low saving',color='r') plt.scatter(x1,y1,label='low income high savings',color='b') plt.xlabel('saving*100') plt.ylabel('income*1000') plt.title('Scatter Plot') plt.legend() plt.show()

As you can see in the above graph, I have plotted two scatter plots based on the inputs specified in the above code. The data is displayed as a collection of points having ‘high-income low salary’ and ‘low-income high salary.’

*Scatter plot with groups*

Data can be classified into several groups. The code below demonstrates:

import numpy as np import matplotlib.pyplot as plt # Create data N = 60 g1 = (0.6 + 0.6 * np.random.rand(N), np.random.rand(N)) g2 = (0.4+0.3 * np.random.rand(N), 0.5*np.random.rand(N)) g3 = (0.3*np.random.rand(N),0.3*np.random.rand(N)) data = (g1, g2, g3) colors = ("red", "green", "blue") groups = ("coffee", "tea", "water") # Create plot fig = plt.figure(figsize=(10,8)) ax = fig.add_subplot(1, 1, 1) for data, color, group in zip(data, colors, groups): x, y = data ax.scatter(x, y, alpha=0.8, c=color, edgecolors='none', s=30, label=group) plt.title('Matplot scatter plot') plt.legend(loc=2) plt.show()

The purpose of using “plt.figure()” is to create a figure object. It’s a Top-level container for all plot elements.

The whole figure is regarded as the figure object. It is necessary to explicitly use “plt.figure()”when we want to tweak the size of the figure and when we want to add multiple Axes objects in a single figure.

fig.add_subplot() is used to control the default spacing of the subplots.

For example, “111” means “1×1 grid, first subplot” and “234” means “2×3 grid, 4th subplot”.

You can easily understand by the following picture:

Next, let us understand the area plot or you can also say Stack plot using python matplotlib.

**Matplotlib: Area Plot**

Area plots are pretty much similar to the line plot. They are also known as stack plots. These plots can be used to display the evolution of the value of several groups on the same graphic. The values of each group are displayed on top of each other. It allows checking on the same figure the evolution of both the total of a numeric variable and the importance of each group.

A line chart forms the basis of an area plot, where the region between the axis and the line is represented by colors.

import numpy as np import matplotlib.pyplot as plt # Your x and y axis x=range(1,6) y=[ [1,4,6,8,9], [2,2,7,10,12], [2,8,5,10,6] ]# Basic stacked area chart. plt.stackplot(x,y, labels=['A','B','C'], colors=['m','c','r']) plt.legend(loc='upper left') plt.show()

import matplotlib.pyplot as plt days = [1,2,3,4,5] Enfield =[50,40,70,80,20] Honda = [80,20,20,50,60] Yahama =[70,20,60,40,60] KTM = [80,20,20,50,60] plt.stackplot(days, Enfield, Honda, Yahama, KTM,labels=['Enfield', 'Honda', 'Yahama', 'KTM'], colors=['r','c','y','m']) plt.xlabel('Days') plt.ylabel('Distance in kms') plt.title('Bikes deatils in area plot') plt.legend() plt.show()

The above-represented graph shows how an area plot can be plotted for the present scenario. Each shaded area in the graph shows a particular bike with the frequency variations denoting the distance covered by the bike on different days. Next, let us move to our last yet most frequently used plot – Pie chart.

**Matplotlib: Pie Chart**

In a pie plot, statistical data can be represented in a circular graph where the circle is divided into portions i.e. slices of pie that denote a particular data, that is, each portion is proportional to different values in the data. This sort of plot can be mainly used in mass media and business.

import matplotlib.pyplot as plt slices = [8,5,5,6] activities = ['Enfield','Honda','Yahama','KTM'] cols = ['c','g','y','b'] plt.pie(slices, labels=activities, colors=cols, startangle=90, shadow= True, explode=(0,0.1,0,0), autopct='%1.1f%%') plt.title('Bike details in Pie Plot') plt.show()

In the above-represented pie plot, the bikes scenario is illustrated, and I have divided the circle into 4 sectors, each slice represents a particular bike and the percentage of distance traveled by it. Now, if you have noticed these slices adds up to 24 hrs, but the calculation of pie slices is done automatically for you. In this way, the pie charts are really useful as you don’t have to be the one who calculates the percentage of the slice of the pie.

**Matplotlib: 3D Plot**

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.

**Line Plot 3D**

import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D fig = plt.figure() ax = fig.add_subplot(111, projection='3d') x = [1,2,3,4,5] y = [50,40,70,80,20] y2 = [80,20,20,50,60] y3 = [70,20,60,40,60] y4 = [80,20,20,50,60] plt.plot(x,y,'g',label='Enfield', linewidth=5) plt.plot(x,y2,'c',label='Honda',linewidth=5) plt.plot(x,y3,'r',label='Yahama',linewidth=5) plt.plot(x,y4,'y',label='KTM',linewidth=5) plt.title('bike details in line plot') plt.ylabel('Distance in kms') plt.xlabel('Days') plt.legend() plt.show()

In the above-represented 3D graph, a line graph is illustrated in a 3-dimensional manner. We make use of a special library to plot 3D graphs which are given in the following syntax.

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.

**Surface Plot 3D**

Axes3D.plot_surface(X, Y, Z, *args, **kwargs)

By default, it will be colored in shades of a solid color, but it also supports color mapping by supplying the *cmap* argument.

The `rstride`

and `cstride`

kwargs set the stride used to sample the input data to generate the graph. If 1k by 1k arrays are passed in, the default values for the strides will result in a 100×100 grid being plotted. Defaults to 10. Raises a ValueError if both stride and count kwargs (see next section) are provided.

from matplotlib import cm from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt import numpy as np fig = plt.figure() ax = fig.gca(projection='3d') ax.set_xlabel('$X-axis$') ax.set_ylabel('$Y-axis$') ax.set_zlabel('$Z-axis$') X = np.arange(-5, 5, 0.25) Y = np.arange(-5, 5, 0.25) X, Y = np.meshgrid(X, Y) R = np.sqrt(X**2 + Y**2) Z = np.sin(R) surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.coolwarm) plt.show()

**Matplotlib: Image Plot**

from pylab import * from numpy import NaN xmin, xmax, ymin, ymax = -2, 0.8, -1.5, 1.5 max_it = 100 # maximum number of iterations px = 3000 # vertical lines res = (ymax - ymin) / px # grid resolution figure(figsize = (10, 10)) def m(c): z = 0 for n in range(1, max_it + 1): z = z**2 + c if abs(z) > 2: return n return NaN X = arange(xmin, xmax + res, res) Y = arange(ymin, ymax + res, res) Z = zeros((len(Y), len(X))) for iy, y in enumerate(Y): print (iy + 1, "of", len(Y)) for ix, x in enumerate(X): Z[-iy - 1, ix] = m(x + 1j * y) save("mandel", Z) # save array to file imshow(Z, cmap = plt.cm.prism, interpolation = 'none', extent = (X.min(), X.max(), Y.min(), Y.max())) xlabel("Re(c)") ylabel("Im(c)") savefig("mandelbrot_python.svg") show()

**Matplotlib: Working With Multiple Plots**

I have discussed multiple types of plots in python matplotlib such as bar plot, scatter plot, pie plot, area plot, etc. Now, let me show you how to handle multiple plots.

import numpy as np import matplotlib.pyplot as plt def f(t): return np.exp(-t) * np.cos(2*np.pi*t) t1 = np.arange(0.0, 5.0, 0.1) t2 = np.arange(0.0, 5.0, 0.02) t3 = np.arange(0.0, 5.0, 0.02) plt.subplot(221) plt.plot(t1, f(t1), 'bo', t2, f(t2)) plt.subplot(222) plt.plot(t2, np.cos(2*np.pi*t2)) plt.subplot(223) plt.plot(t3, np.tan(2*np.pi*t2)) plt.show()