# How to Plot Multiple Graphs in Python Matplotlib?

Today in this tutorial, we will see how we can plot multiple graphs in python using the Matplotlib library.

The Matplotlib is one of the most used library for data visualization. This means you can plot any type of chart like columns chart, line chart, bar chart, pie chart, scatter chart, etc.

So we will use this Matplotlib library to draw the multiple graphs in a single plot. Let’s start with the tutorial, first, we will see how to plot a single graph using Matplotlib and then how we can draw multiple graphs on the same plot.

## Plot Single Graph Using Matplotlib

To drow the single plot graph in python, you have to first install the Matplotlib library and then use the `plot()` function of it.

`pip install matplotlib`

After installation, now we will import it into a python file and use the `plot()` function to draw the simple graph.

```import matplotlib.pyplot as plt
import numpy as np

# Data for plotting
x = np.arange(0.0, 2.0, 0.01)
y = 1 + np.sin(2 * np.pi * x)

fig, ax = plt.subplots()
ax.plot(x, y)

ax.set(xlabel='time (s)', ylabel='voltage (mV)',
title='Voltage frequency by timing')

ax.grid()

fig.savefig("test-plot.png")

plt.show()```

In the output of the above code, you will see the graph like the following image.

So in the above code, first we defined the x-axis and y-axis values using the NumPy. Then we used the `plot()` function to draw the graph on canvas.

Set the labels for the x-axis and y-axis using the `set()` function, you can also use the direct `xlabel()` and `ylabel()` functions.

And in the last, we set the main title of the plot and then show the graph on canvas using the `show()` function, and for saving images used the `savefig()` function.

##### Let’s see one more example of a simple plot in python.
```import matplotlib.pyplot as plt

x = [5,7,1]
y = [9,2,6]

plt.plot(x, y)

plt.xlabel('x - axis')
plt.ylabel('y - axis')

plt.title('Simple Plot in Python')

plt.show()```

Now in this above, we took another example by taking x-axis and y-axis values and using the direct `xlabel()` and `ylabel()` function to set the labels for both side.

Let’s now see how we can plot multiple graphs in python using the Matplotlib library. Moreover, we will see some useful examples.

## Plot Multiple Graphs Using Matplotlib

Now we will discuss how we can use the matplotlib library to draw the multiple graphs in python. Plot multiple means we can draw multiple graphs on the same canvas with the same code script.

So we will use the `subplot()` or `subplots()` function of matplotlib library to draw the multiple graphs. See the syntax of it.

```matplotlib.pyplot.subplot(nrows, ncols, index, **kwargs)

//OR

matplotlib.pyplot.subplots(nrows=1, ncols=1, sharex=False, sharey=False, squeeze=True, subplot_kw=None, gridspec_kw=None, **fig_kw)```

Let’s see the examples of both functions.

#### Using subplots() Function

The `subplots()` function is the main wrapper function that allows us to plot more than one graph on a single canvas by calling it once.

Let’s see the example of plotting bar chart, scatter chart, and line chart using the `subplots()` function of matplotlib which will have one main title.

##### Example 1
```import matplotlib.pyplot as plt

data = {apple': 10, 'orange': 15, 'lemon': 5, 'lime': 20}

x = list(data.keys())
y = list(data.values())

fig, axs = plt.subplots(1, 3, figsize=(9, 3), sharey=True)

axs[0].bar(x, y)
axs[1].scatter(x, y)
axs[2].plot(x, y)

fig.suptitle('Categorical Plotting')```

##### Example 2
```import numpy as np
import matplotlib.pyplot as plt

np.random.seed(24693023)

dt = 0.01
t = np.arange(0, 30, dt)
n1 = np.random.randn(len(t))
n2 = np.random.randn(len(t))

s1 = np.sin(2 * np.pi * 10 * t) + n1
s2 = np.sin(2 * np.pi * 10 * t) + n2

fig, axs = plt.subplots(2, 1)

axs[0].plot(t, s1, t, s2)
axs[0].set_xlim(0, 2)
axs[0].set_xlabel('time')
axs[0].set_ylabel('s1 and s2')
axs[0].grid(True)

cxy, f = axs[1].cohere(s1, s2, 256, 1. / dt)
axs[1].set_ylabel('coherence')

fig.tight_layout()
plt.show()```

You can see the more examples of `subplots()` function on the matplotlib.org site.