Matplotlib Tutorial: Learn basics of Python’s powerful Plotting library

Matplotlib Tutorial: Learn basics of Python’s powerful Plotting libraryKillol GovaniBlockedUnblockFollowFollowingFeb 3What is MatplotlibTo make necessary statistical inferences, it becomes necessary to visualize your data and Matplotlib is one such solution for the Python users.

It is a very powerful plotting library useful for those working with Python and NumPy.

The most used module of Matplotib is Pyplot which provides an interface like MATLAB but instead, it uses Python and it is open source.

Installing MatplotlibTo install Matplotlib on your local machine, open Python command prompt and type following commands:python -m pip install -U pippython -m pip install -U matplotlibI am assuming that you wish to foray into the world of data science and machine learning and hence I suggest you download the anaconda package distribution from here.

It installs python, Jupyter notebook and other important python libraries including Matplotlib, Numpy, Pandas, scikit-learn.

Anaconda supports Windows, MacOS and Linux.

To quickly get started with Matplotlib without installing anything on your local machine, check out Google Colab.

It provides the Jupyter Notebooks hosted on the cloud for free which are associated with your Google Drive account and it comes with all the important packages pre-installed.

You can also run your code on GPU which helps in faster computation though we don’t need GPU computation for this tutorial.

To quickly get started with Google Colab, check out this amazing article.

General ConceptsA Matplotlib figure can be categorized into several parts as below:Figure: It is a whole figure which may contain one or more than one axes (plots).

You can think of a Figure as a canvas which contains plots.

Axes: It is what we generally think of as a plot.

A Figure can contain many Axes.

It contains two or three (in the case of 3D) Axis objects.

Each Axes has a title, an x-label and a y-label.

Axis: They are the number line like objects and take care of generating the graph limits.

Artist: Everything which one can see on the figure is an artist like Text objects, Line2D objects, collection objects.

Most Artists are tied to Axes.

Getting Started with PyplotPyplot is a module of Matplotlib which provides simple functions to add plot elements like lines, images, text, etc.

to the current axes in the current figure.

Make a simple plotimport matplotlib.

pyplot as pltimport numpy as npHere we import Matplotlib’s Pyplot module and Numpy library as most of the data that we will be working with will be in the form of arrays only.

We pass two arrays as our input arguments to Pyplot’s plot() method and use show() method to invoke the required plot.

Here note that the first array appears on the x-axis and second array appears on the y-axis of the plot.

Now that our first plot is ready, let us add the title, and name x-axis and y-axis using methods title(), xlabel() and ylabel() respectively.

We can also specify the size of the figure using method figure() and passing the values as a tuple of the length of rows and columns to the argument figsizeWith every X and Y argument, you can also pass an optional third argument in the form of a string which indicates the colour and line type of the plot.

The default format is b- which means a solid blue line.

In the figure below we use go which means green circles.

Likewise, we can make many such combinations to format our plot.

We can also plot multiple sets of data by passing in multiple sets of arguments of X and Y axis in the plot() method as shown.

Multiple plots in one figure:We can use subplot() method to add more than one plots in one figure.

In the image below, we used this method to separate two graphs which we plotted on the same axes in the previous example.

The subplot() method takes three arguments: they are nrows, ncols and index.

They indicate the number of rows, number of columns and the index number of the sub-plot.

For instance, in our example, we want to create two sub-plots in one figure such that it comes in one row and in two columns and hence we pass arguments (1,2,1) and (1,2,2) in the subplot() method.

Note that we have separately used title()method for both the subplots.

We use suptitle() method to make a centralized title for the figure.

If we want our sub-plots in two rows and single column, we can pass arguments (2,1,1) and (2,1,2)The above way of creating subplots becomes a bit tedious when we want many subplots in our figure.

A more convenient way is to use subpltots() method.

Notice the difference of ‘s’ in both the methods.

This method takes two arguments nrows and ncols as number of rows and number of columns respectively.

This method creates two objects:figure and axes which we store in variables fig and ax which can be used to change the figure and axes level attributes respectively.

Note that these variable names are chosen arbitrarily.

Creating different types of graphs with Pyplot1) Bar GraphsBar graphs are one of the most common types of graphs and are used to show data associated with the categorical variables.

Pyplot provides a method bar() to make bar graphs which take arguments: categorical variables, their values and color (if you want to specify any).

To make horizontal bar graphs use method barh() Also we can pass an argument (with its value)xerr oryerr (in case of the above vertical bar graphs) to depict the variance in our data as follows:To create horizontally stacked bar graphs we use the bar() method twice and pass the arguments where we mention the index and width of our bar graphs in order to horizontally stack them together.

Also, notice the use of two other methods legend() which is used to show the legend of the graph and xticks() to label our x-axis based on the position of our bars.

Similarly, to vertically stack the bar graphs together, we can use an argument bottom and mention the bar graph which we want to stack below as its value.

2) Pie ChartsOne more basic type of chart is a Pie chart which can be made using the method pie() We can also pass in arguments to customize our Pie chart to show shadow, explode a part of it, tilt it at an angle as follows:3) HistogramHistograms are a very common type of plots when we are looking at data like height and weight, stock prices, waiting time for a customer, etc which are continuous in nature.

Histogram’s data is plotted within a range against its frequency.

Histograms are very commonly occurring graphs in probability and statistics and form the basis for various distributions like the normal -distribution, t-distribution, etc.

In the following example, we generate a random continuous data of 1000 entries and plot it against its frequency with the data divided into 10 equal strata.

We have used NumPy’s random.

randn() method which generates data with the properties of a standard normal distribution i.


mean = 0 and standard deviation = 1, and hence the histogram looks like a normal distribution curve.

4)Scatter Plots and 3-D plottingScatter plots are widely used graphs, especially they come in handy in visualizing a problem of regression.

In the following example, we feed in arbitrarily created data of height and weight and plot them against each other.

We used xlim() and ylim() methods to set the limits of X-axis and Y-axis respectively.

The above scatter can also be visualized in three dimensions.

To use this functionality, we first import the module mplot3d as follows:from mpl_toolkits import mplot3dOnce the module is imported, a three-dimensional axes is created by passing the keyword projection='3d' to the axes() method of Pyplot module.

Once the object instance is created, we pass our arguments height and weight to scatter3D() method.

We can also create 3-D graphs of other types like line graph, surface, wireframes, contours, etc.

The above example in the form of a simple line graph is as follows: Here instead of scatter3D() we use method plot3D()SummaryHope this article was useful to you.

If you liked this article please express your appreciation.

Before we end the article here is the list of all the methods as they appeared.

plot(x-axis values, y-axis values) — plots a simple line graph with x-axis values against y-axis valuesshow() — displays the graphtitle(“string”) — set the title of the plot as specified by the stringxlabel(“string”) — set the label for x-axis as specified by the stringylabel(“string”) — set the label for y-axis as specified by the stringfigure() — used to control a figure level attributessubplot(nrows, ncols, index) — Add a subplot to the current figuresuptitle(“string”) — It adds a common title to the figure specified by the stringsubplots(nrows, ncols, figsize) — a convenient way to create subplots, in a single call.

It returns a tuple of a figure and number of axes.

set_title(“string”) — an axes level method used to set the title of subplots in a figurebar(categorical variables, values, color) — used to create vertical bar graphsbarh(categorical variables, values, color) — used to create horizontal bar graphslegend(loc) — used to make legend of the graphxticks(index, categorical variables) — Get or set the current tick locations and labels of the x-axispie(value, categorical variables) — used to create a pie charthist(values, number of bins) — used to create a histogramxlim(start value, end value) — used to set the limit of values of the x-axisylim(start value, end value) — used to set the limit of values of the y-axisscatter(x-axis values, y-axis values) — plots a scatter plot with x-axis values against y-axis valuesaxes() — adds an axes to the current figureset_xlabel(“string”) — axes level method used to set the x-label of the plot specified as a stringset_ylabel(“string”) — axes level method used to set the y-label of the plot specified as a stringscatter3D(x-axis values, y-axis values) — plots a three-dimensional scatter plot with x-axis values against y-axis valuesplot3D(x-axis values, y-axis values) — plots a three-dimensional line graph with x-axis values against y-axis values.. More details

Leave a Reply