Python Matplotlib With Examples

Python matplotlib library helps us to plot data on graphs in its simplest terms. If you are familiar with MATLAB plotting, then Matplotlib will be easy to use for basic plotting.

Python Matplotlib

To start understanding how Matplotlib helps us building graphs and visualisation figures to represent data, we will need to know some of the basic terms we will use a lot in this post. Let’s study these terms first.

Python Matplotlib Terminology

  • The Figure is complete window or the page the graph is drawn upon.
  • The Axes is the area on which data is plotted. This can be X-Axis or Y-Axis etc.
  • The Spines are the lines which connects Axes points.

Install Matplotlib

It is easy to install python matplotlib library with pip:

That’s it! Now we are ready to build some cool examples using this data visualisation library.

Getting started with Matplotlib

In this section, we will get started with the plot construction and start feeding data to python matplotlib functions.

Matplotlib Line Plot

We will start with a very basic example of plotting. We will just use two Python lists as the data source for points of a graph. Let’s write a code snippet for this:

Note the last line with the show() function. It is important to call it otherwise, the plot won’t be shown on the screen. When we run this code, we can see the following figure appear:
Python matplotlib
Note that we can also provide a title to this figure and labels to our Axes by using this snippet:

Matplotlib Scatter Plot

Above plot was very much indicative of the points which were actually not passed in the array as it showed a line. What if we only want to see the actual points on the plot? Scatter plot achieves this:

When we run this code, we can see the following figure appear:

python-matplotlib moved spine legend

Matplotlib Historgrams

In this section, we introduce you to Histograms. While graphs inform us how our data vary, histogram describes how our data is distributed. More the values in a range, higher the bar for a range.

We use hist() function to make a Histogram. It has 2 important parameters:

  • List of values to plot
  • Number of ranges to distribute these points into

Let’s demonstrate this with a code snippet:

When we run this code, we can see the following figure appear:
Python matplotlib
The default value for number of bins is 10. The number of bins is important to set. Smaller number of bins can hide reality of data distribution and too many bins can overcomplicate reality.

Customisation in Matplotlib Plot

If you notice the first Line plot, we see that Y-axis didn’t started from 0. We can modify this:

When we run this code, we can see the following figure appear:

Python matplotlib

Drawing multiple curves in Matplotlib

It is utterly common to draw multiple curves on a single graph to make a comparison. Let’s try this here:

When we run this code, we can see the following figure appear:
python-matplotlib moved spine legend
So, it was just a matter of calling plot multiple times. To add, we used numpy to create a non-linear curve!

Changing color and Adding Legends in Matplotlib Plot

As we saw, curves look nice but aren’t they all look so, similar? What if we want to change their color and show what each color represents? Let’s try drawing the sine and cosine curves together:

When we run this code, we can see the following figure appear:
python-matplotlib moved spine legend
If you notice, we actually did two things in this figure:

  1. Modified the color for the curves to make the comparison easier
  2. Added a legend frame which introduces which colour represents what. This makes the metadata on the graph very easy to read.

Creating Bar chart in Matplotlib

We can create appealing bar charts with Matplotlib with simple code snippet:

When we run this code, we can see the following figure appear:

Python matplotlib

Creating Pie chart in Matplotlib

We can create appealing pie charts with Matplotlib with simple code snippet:

When we run this code, we can see the following figure appear:
Python matplotlib
See how we elevated one of the slice in the pie chart to differentiate it from the rest!

Creating Heat Maps in Matplotlib

Charts are cool but when it comes to visualising geographical information, nothing works better than a heat map:

When we run this code,

Note that we created the data by just random values and output figure can vary depending on the values.

That’s all for python matplotlib plotting tutorial.

Reference: Website

By admin

Leave a Reply

%d bloggers like this: