How to Create Subplots of Graphs in Matplotlib with Python 


               


How to Create Subplots of Graphs in Matplotlib with Python



Python


In this article, we show how to create subplots of graphs in matplotlib with Python.

So with matplotlib, the heart of it is to create a figure.

On this figure, you can populate it with all different types of data, including axes, a graph plot, a geometric shape, etc.

However, it doesn't simply have to have one graph plot. It can have several.

A subplot is when the graph is broken up into more than 1 plot.

You can have as many subplots as you want.

Subplots are created by the number you want in rows and columns.

Thus, if you specify you want to create subplots composed of 2 rows with 3 graphs in a row, you would set the rows equal to 2 and the columns equal to 3.

This is shown in the following code below.



So the first thing we have to do is import matplotlib. We do this with the line, import matplotlib.pyplot as plt

We then create a variables, fig and axes and set it equal to, plt.subplots(nrows=2,ncols=3)

What this does is it makes the fig and axes variables subplots. The nrows sets the number of rows of graphs there will be and the ncols sets the number of graphs per rows there will be. In this case, I set nrows equal to 2, meaning that there will be 2 rows of graphs. ncols set to 3, meaning there will be 3 graphs per row.

plt.tight_layout() makes sure that each individual graph does not overlap with other graphs. The function, tight_layout() spaces out the graphs so that the axes of each graph doesn't collide with another.

We then use the function, plt.show(), to show the graph.

This works if you're using a python IDE other than jupyter notebooks. If you are using jupyter notebooks, then you would not use, plt.show(). Instead you would specify in the code right after importing matplotlib, %matplotlib inline

This line allows the figure of a graph to be shown with jupyter notebooks.

After running the following code above, we get the following figure with axes shown in the image below.


Figure object with subplots with matplotlib in Python


So now you see a figure object with 6 subplots on it, 2 rows and 3 graphs per row.

One more thing to go through now.

Right now we have multiple subplots created.

Now how do we plot graphs on these subplots?

Of course we're not going to want empty subplots?

So we can plot graphs of subplots using the plot() function.

To specify which graph you want to plot, you choose, axes followed by the row and column.

Since we created subplots of 2 rows with 3 graphs in each row (3 columns), then there are 6 graphs.

To reference the graph on row 1 column 1, we use the statement, axes[0][0]

To reference the graph on row 1 column 2, we use the statement, axes[0][1]

To reference the graph on row 1 column 3, we use the statement, axes[0][2]

To reference the graph on row 2 column 1, we use the statement, axes[1][0]

To reference the graph on row 2 column 2, we use the statement, axes[1][1]

To reference the graph on row 2 column 3, we use the statement, axes[1][2]

In the following code below, we plot a graph on the second graph of the first row and on the 1st graph on the second row.



So in this code, we create subplots of 2 rows with each row having 3 graphs.

We then have an x-axis composed of numbers 1 to 5.

The y-axis is the square numbers of all the x numbers, so it's 1 to 25.

We then do the graph plot of the x and y numbers on the second graph on row 1.

We then do teh graph plot of the y and x numbers on the 1st graph on row 2.

We add titles to the graph.

You can see this in the graph below.




Figure object with subplots with graph plots with matplotlib in Python




So now you see how to plot graphs on subplots.

And this is how to create subplots in matplotlib with Python.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...