Plotting Subplots in Matplotlib šš
ā¢ One of the handiest ways to plot multiple charts in matplotlib I find is using the subplot() method.
ā¢ It can be used with matplotlib as well as seaborn plots just as well.
Let's see how to use it ā
ā¢ First, calculate the total plots you want.
ā¢ Then we decided what grid we want, the numbers have to be multiple of the total plots.
ā¢ Like for 4 plots we can have 4x1, 2x2, 1x4 grids.
ā¢ To plot, initialize the main plot canvas that would have all the subplots using figure() method.
ā¢ Add the size you want for the total canvas.
ā¢ Then for each plot precede the plot function with the subplot() method.
ā¢ The subplot method would take three arguments, the first multiple (rows), the second multiple (columns), and plot no.
ā¢ If we want 4 plots, in 1x4, it would be written as subplot(1,4,1), (1,4,2)...
ā¢ Individual elements like legend and title would work for preceding plot until next subplot() is encountered.
ā¢ We can put the show() method at the end to suppress any intermediate outputs.
Here are some examples of 4 plots in different grid sets.
ā¢ Using 1x4 Grid
ā¢ Using 2 x 2 Grid
ā¢ Using 4x1 Grid
ā¢ Keep in mind that you'd need to adjust the figure size as per the grid.
ā¢ Like for a 4x1 grid you'd want more height while for 1x4 you'd need more width for better readability.
So that's a quick way to create subplots using matplotlib!
Any comments, suggestions or corrections are welcome.
Thanks for reading!