How to use Subplots in matplotlib Data Visualization using Python

Many a times, I needed multiple plots in the same view as it helps in analyzing data in proper way. Also, if you want to present this data somewhere, it helps to plot two graphs together.

One alternative to subplots is using multiple axis and plotting 2 data points on the same graph but this might distort the view. Each has its own applications. In this article, we will look at how to plot  subplots.

Sytax is as below

subplot(nrows ncols index)

Here, nrows and ncols need not be same for all plots. Its only specific to current plot. Please refer to third example in this article

 

  • Plotting three graphs using matplotlib

Output is as below

 

  • Plotting 4 graphs in 2*2 grid using matplotlibb

Output is as below

  • Plotting 3 graphs using matplotlib

Output is as below

If you have noticed, in third example, subplot columns are different as compared to first one. This is because, we wanted to plot a single graph in second row.

Also note that we have used sharex=ax1, sharey=ax1 to maintain same scale on all columns. If scales are different, it does not gives us relative analysis.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.