Top 5 most helpful Matplotlib tricks

Saad Raees
6 min readJan 28, 2021

Matplotlib is the first visualization library we get to with in Python. And that’s really no surprise — Matplotlib is a core Python library that helps us explore our data with aesthetically pleasing visualization. Who doesn’t like that?

Whether you’re working as a data analyst, a business analyst, or a data scientist, you should be familiar with the power of matplotlib. Matplotlib helps us tell stories in an effective and impactful manner, thus tying our analysis to business goals.

There’s no doubt that matplotlib is an easy and beginner-friendly library. But there can be times when things become complicated, especially if you’re a newcomer to this wonderful Python library.

So I decided to put together a list of 5 widely asked questions about matplotlib that I feel every analyst should know. Consider these as handy matplotlib tricks, or data visualization tips, to create impactful stories and charts.

matplotlib Trick #1 — How to Change Plot Size?

The first is the easiest tricks in this article but it is the most searched question on Stackoverflow related to matplotlib. Changing the size of the plot is quite important at times given how it can vary based on various situations.

We can use the figure() class of matplotlib for creating a new figure. The same can be used for changing the size of the plot. You just need to pass the width and height of the desired plot in inches as the figsize argument:

Here you can see that the same line plot looks much better when presented with a different set of dimensions. I have used random functions here and I’ll be using them later too. Therefore, I recommend you to read this article:

matplotlib Trick #2 — How to generate subplots?

A subplot is a figure having multiple plots in it. This is the concept most analysts struggle with initially. Therefore, I thought it is best to discuss it here.

There are three ways of generating subplots in matplotlib:

  1. Using subplot()
  2. Using subplots()
  3. Using subplot2grid()

Let’s understand how you can use each of them.

Using subplot() is the easiest and most explicit way of creating subplots in matplotlib. It takes three things — the number of rows, number of columns, and index — as arguments and returns an axes object, which you can then use for plotting.

Here, you can see subplot() works flawlessly. But notice that for each plot in the figure, we have to write a similar statement again and again, which doesn’t make it a suitable choice when we have to create multiple subplots.

That’s where the subplots() function comes into the picture. Instead of returning one axes object at a time, it returns an array. You just need to pass the number of rows and columns as arguments to subplots(), and it will return an array of axes objects.

You might be thinking — all these plots are of the same size, what if I have to modify them? Let’s answer this question.

subplot2grid() allows you to treat the figure as a grid and modify your plots based on the rows and columns of the grid. It takes shape, location, rowspan, and columnspan as arguments and returns axes object for a specific location and of a particular size in a grid:

matplotlib Trick #3 — How to annotate plots?

Annotation is a comment added to a plot at a point for making it more understandable.

Annotations are of great help in marking different positions of a plot. There are two ways of doing this in matplotlib:

  • Using the text() function
  • Using the annotate() function of pyplot

Let’s start with the text() function.

Let’s say you have to mention values over each bar of a bar plot. text() function can be beneficial here because it takes x and y position as arguments and lets you write text there:

Now, let’s say you want to annotate a point on the plot with a marker and also write text. Here, the annotate() function can be used:

You can see above that the annotate() function also allows you to create arrows,. That’s what makes it superior to the text() function because text() function can only be used for text. You can read more about the annotate() function here:

matplotlib Trick #4 — How to modify axes?

Let’s go back to our previous plot. You will notice that there are some spaces left around the axes of the plot. We can correct this by modifying the axes of the plot. xlim() and ylim() are the two functions used for changing the limit of axes of a plot:

The empty spaces are removed from the plot but it still doesn’t look clean. Let’s do one thing — remove some of the axes of the plot. We can do this by manually setting the visibility of the spines of the axes to False:

Here, the plt.gca() is used for accessing the current axes object. Now our plot looks much cleaner but not quite aesthetically pleasing. We can try to make it more appealing by changing the color of the axes.

You can do this by setting the color of the spines manually using the set_color() function:

Looking good!

matplotlib Trick #5 — How to make plots interactive?

This is perhaps the most anticipated trick of the article. And it’s also the easiest thing you’ll ever do in matplotlib.

You can make any matplotlib plot interactive in a Jupyter notebook by just writing one line of code, i.e., %matplotlib notebook. Let’s try it on our previous plot:

You can see above that now we have the options to zoom, pan, and save our plot. Also, our axes are now dynamic and can change themselves based on the movement of the cursor.

Note: This works only in Jupyter Notebooks.

--

--