# Overlay Plots in Matplotlib

This tutorial will teach you how to overlay plots in Matplotlib.

Before working with plots, we need to set up our script to work with the library. So we start by importing matplotlib.

Furthermore, we load the `randrange` function from the `random` module to quickly generate some data. Remember that it will look different for you.

``````import matplotlib.pyplot as plt
from random import randrange

data_1 = [randrange(0, 10) for _ in range(0, 10)]
data_2 = [randrange(0, 10) for _ in range(0, 10)]
``````

## Overlay Plots in Matplotlib

If you want to have multiple plots, we can easily add more. In the following code, we generate a line plot and a bar.

We apply some color to it to see the difference more clearly.

``````plt.plot(data_1, label="Random Data", c="Red")
plt.bar(data_2, data_1, label="Random Data")

plt.show()
``````

Keep in mind the differences in data since we randomly generated it.

Output:

## Set Plot Layer With `zorder` in Matplotlib

As you saw in the image, the red line is in Front of the Bars, but you may want to change that. To do that, we use the `zorder` parameter.

We have to provide an int that corresponds with the layer. Keep in mind; 2 will show in front of 1.

``````plt.plot(data_1, label="Random Data", c="Red", zorder=0)
plt.bar(data_2, data_1, label="Random Data", zorder=1)

plt.show()
``````

Output:

Complete code:

``````import matplotlib.pyplot as plt
from random import randrange

data_1 = [randrange(0, 10) for _ in range(0, 10)]
data_2 = [randrange(0, 10) for _ in range(0, 10)]

plt.plot(data_1, label="Random Data", c="Red", zorder=9)
plt.bar(data_2, data_1, label="Random Data", zorder=10)

plt.show()
``````
Author: Maxim Maeder

Hi, my name is Maxim Maeder, I am a young programming enthusiast looking to have fun coding and teaching you some things about programming.

GitHub

## Related Article - Matplotlib Plot

• Matplotlib Contour Plot
• Matplotlib Candlestick Plot
• Matplotlib Waterfall Plot
• Plot Dates in Matplotlib
• Matplotlib Density Plot