In Matplotlib, we can draw multiple graphs in a single plot in two ways. One is by using subplot() function and other by superimposition of second graph on the first i.e, all graphs will appear on the same plot. We will look into both the ways one by one.
Multiple Plots using subplot () Function
A subplot () function is a wrapper function which allows the programmer to plot more than one graph in a single figure by just calling it once.
Syntax: matplotlib.pyplot.subplots(nrows=1, ncols=1, sharex=False, sharey=False, squeeze=True, subplot_kw=None, gridspec_kw=None, **fig_kw)
- nrows, ncols: These gives the number of rows and columns respectively. Also, it must be noted that both these parameters are optional and the default value is 1.
- sharex, sharey: These parameters specify about the properties that are shared among a and y axis.Possible values for them can be, row, col, none or default value which is False.
- squeeze: This parameter is a boolean value specified, which asks the programmer whether to squeeze out, meaning remove the extra dimension from the array. It has a default value False.
- subplot_kw: This parameters allow us to add keywords to each subplot and its default value is None.
- gridspec_kw: This allows us to add grids on each subplot and has a default value of None.
- **fig_kw: This allows us to pass any other additional keyword argument to the function call and has a default value of None.
In Matplotlib, there is another function very similar to subplot which is subplot2grid (). It is same almost same as subplot function but provides more flexibility to arrange the plot objects according to the need of the programmer.
This function is written as follows:
Syntax: matplotlib.pyplot.subplot2grid(shape, loc, rowspan=1, colspan=1, fig=None, **kwargs)
This parameter is a sequence of two integer values which tells the shape of the grid for which we need to place the axes. The first entry is for row, whereas the second entry is for column.
Like shape parameter, even Ioc is a sequence of 2 integer values, where first entry remains for the row and the second is for column to place axis within grid.
This parameter takes integer value and the number which indicates the number of rows for the axis to span to or increase towards right side.
This parameter takes integer value and the number which indicates the number of columns for the axis to span to or increase the length downwards.
This is an optional parameter and takes Figure to place axis in. It defaults to current figure.
This allows us to pass any other additional keyword argument to the function call and has a default value of None.
Plotting in same plot
We have now learnt about plotting multiple graphs using subplot and subplot2grid function of Matplotlib library. As mentioned earlier, we will now have a look at plotting multiple curves by superimposing them. In this method we do not use any special function instead we directly plot the curves one above other and try to set the scale.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.