How do i plot multiple plots in one python?

Prerequisites: Matplotlib

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)

Parameters:

  1. 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.
  2. 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.
  3. 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.
  4. subplot_kw: This parameters allow us to add keywords to each subplot and its default value is None.
  5. gridspec_kw: This allows us to add grids on each subplot and has a default value of None.
  6. **fig_kw: This allows us to pass any other additional keyword argument to the function call and has a default value of None.

Example :

Python3

import matplotlib.pyplot as plt

import numpy as np

import math

X = np.arange(0, math.pi*2, 0.05)

Y1 = np.sin(X)

Y2 = np.cos(X)

Y3 = np.tan(X)

Y4 = np.tanh(X)

figure, axis = plt.subplots(2, 2)

axis[0, 0].plot(X, Y1)

axis[0, 0].set_title("Sine Function")

axis[0, 1].plot(X, Y2)

axis[0, 1].set_title("Cosine Function")

axis[1, 0].plot(X, Y3)

axis[1, 0].set_title("Tangent Function")

axis[1, 1].plot(X, Y4)

axis[1, 1].set_title("Tanh Function")

plt.show()

Output

How do i plot multiple plots in one python?

Multiple plots using subplot() function

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)

Parameter:

  1. shape
    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.
  2. loc
    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.
  3. rowspan
    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.
  4. colspan
    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.
  5. fig
    This is an optional parameter and takes Figure to place axis in. It defaults to current figure.
  6. **kwargs
    This allows us to pass any other additional keyword argument to the function call and has a default value of None.

Example :

Python3

import matplotlib.pyplot as plt

import numpy as np

import math

plot1 = plt.subplot2grid((3, 3), (0, 0), colspan=2)

plot2 = plt.subplot2grid((3, 3), (0, 2), rowspan=3, colspan=2)

plot3 = plt.subplot2grid((3, 3), (1, 0), rowspan=2)

x = np.arange(1, 10)

plot2.plot(x, x**0.5)

plot2.set_title('Square Root')

plot1.plot(x, np.exp(x))

plot1.set_title('Exponent')

plot3.plot(x, x*x)

plot.set_title('Square')

plt.tight_layout()

plt.show()

Output

How do i plot multiple plots in one python?

Multiple Plots using subplot2grid() function

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.

Example :

Python3

import matplotlib.pyplot as plt

import numpy as np

import math

X = np.arange(0, math.pi*2, 0.05)

y = np.sin(X)

z = np.cos(X)

plt.plot(X, y, color='r', label='sin')

plt.plot(X, z, color='g', label='cos')

plt.xlabel("Angle")

plt.ylabel("Magnitude")

plt.title("Sine and Cosine functions")

plt.legend()

plt.show()

Output

How do i plot multiple plots in one python?

sine and cosine function curve in one graph


How do you plot multiple graphs in Python?

Create x, y1 and y2 data points using numpy..
Add a subplot to the current figure at index 1..
Plot curve 1 using x and y1..
Add a subplot to the current figure at index 2..
Plot curve 2 using x and y2..
To display the figure, use show() method..

How do you combine two graphs in Python?

MatPlotLib with Python.
Set the figure size and adjust the padding between and around the subplots..
Create x, y1 and y2 data points using numpy..
Plot (x, y1) and (x, y2) points using plot() method..
Get the xy data points of the current axes..
Use argsort() to return the indices that would sort an array..

How do you plot multiple graphs on a grid in Python?

To draw multiple plots using the subplot() function from the pyplot module, you need to perform two steps: First, you need to call the subplot() function with three parameters: (1) the number of rows for your grid, (2) the number of columns for your grid, and (3) the location or axis for plotting.

How do you plot 4 subplots in Python?

subplots=True and layout , for each column. Use the parameters subplots=True and layout=(rows, cols) in pandas.DataFrame.plot. ... .
plt. subplots , for each column. ... .
plt. subplots , for each group in . ... .
seaborn figure-level plot. Use a seaborn figure-level plot, and use the col or row parameter..