Matplotlib Zorder Explained with Examples

Hello geeks and welcome in this article, we will cover Matplotlib Zorder. Along with that, we look at its syntax, what difference does it make to the graph. To do so, we will look at a couple of examples. But first, let us try to generate a general overview of the function. Matplotlib is the plotting library for the Python programming language.

The Zorder attribute of the Matplotlib Module helps us to improve the overall representation of our plot. This property determines how close the points or plot is to the observer. The higher the value of Zorder closer the plot or points to the viewer. Things will become more clear as we move ahead in this article.

Zorder Implementation in Matplotlib

In this section, we will learn how to implement the Zorder. We will also notice what difference does the addition of Zorder makes.

Let us look at a simple example

import matplotlib.pyplot as plt
plt.plot([2,4],[4,4])
plt.plot([1,5],[2,6])
plt.show()
Matplotlib Zorder

Here we can see a simple plot with lines. Here we have considered 2 different set points. They are joined together to form a straight line. In order to execute this program, we have just imported the Matplotlib. Then we have declared points and at last, used the plt. show() tag to get the output.

But in the above graph straight line from a point having coordinates (1,2) and (5,6). Appear over (2,4) and (4,4). We wish to change that and reverse the order. Let us see how we can do that

import matplotlib.pyplot as plt
plt.plot([2,4],[4,4],zorder=2,linewidth=20)
plt.plot([1,5],[2,6],zorder=1,linewidth=20)
plt.show()
Zorder

Here we can see that using the Zorder we are able to achieve the desired output. Here in order to get a clear cut view I have also increased the line width to 20. Now, this Zorder can be executed for any number of lines.

Now let us look at how it works for a 4 straight lines graph.

import matplotlib.pyplot as plt
plt.plot([2,4],[4,4],zorder=5,linewidth=10)
plt.plot([1,5],[2,6],zorder=1,linewidth=10)
plt.plot([1,4],[2,4],zorder=3,linewidth=10)
plt.plot([2,5],[1,3.5],zorder=4,linewidth=10)
plt.plot([3,1],[5,1],zorder=2,linewidth=10)
plt.show()
Multi Zorder

Here we can see that Zorder works fine even for this program. By default even if the Zorder is not declared. The program itself follows the following order

TypeZorder
Patch collection1
Line collection2
Text3

Let us verify this through an example

import matplotlib.pyplot as plt
import numpy as np
x = np.array([1, 2, 5, 4])
y = np.array([ 2.65, 2.65, 4, 2 ])
m, b = np.polyfit(x, y, 1)
plt.xlabel("X-axis")
plt.ylabel("y-axis")

plt.plot(x, y, 'o')
plt.plot(x, m*x + b)
plt.show()
scatter plot

Above we can see an example where have plotted a scatter plot and straight line between it. In order to do so, we require the NumPy module along with matplotlib. I hope that how to implement and how does Zorder will be clear to you. In the next section, we will discuss some of the general questions related to this function.

Can we set zorder for twinx grid in Matplotlib?

There is no easy way of setting a zorder for the twinx grid. The reason for this being is that the twinx grid creates two separate axes for the graphs. Zorder is only bound to work on one single axis. But there is one workaround solution that uses the existing zorder of one graph and uses it to set the zorder of another graph. Following code will help you to understand it –

Code:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import ticker as tick

x = [1, 2, 3, 4, 5, 6]
y1 = [1, 2, 3, 1, 2, 3]
y2 = [3, 2, 1, 3, 2, 1]


figure = plt.figure(figsize=(5.15, 5.15))
figure.clf()
plot = plt.subplot(111)
plot.plot(x, y1, zorder = 2)
primary_ticks = len(plot.yaxis.get_major_ticks())
ax2 = plot.twinx()
ax2.plot(x, y2, color='red', zorder = 1)
ax2.grid(False)
ax2.yaxis.set_major_locator(tick.LinearLocator(primary_ticks))
plot.set_zorder(ax2.get_zorder()+1)
plot.patch.set_visible(False)
plt.show()

Output:

set zorder for twinx grid in Matplotlib

Explanation:

By using plot.set_zorder() we changed the zorder of plot, which was below the ax2 to the top. Twinx graph is coded so that always ax2 will have a higher zorder than its old twin. But you can use the set_zorder on the old twin to bring it on top.

General Questions

1. How to change the color of the lines in Zorder?

Ans. In order to change the color of the lines in the plot, all you have to do is add the “color” in the syntax. After that, you can specify the color. You can use hex-code, RBGA, or just simply write the color.

2. How to change the width of the lines of the plot?

Ans. In order to do so, you need to use the “linewidth” parameter. After which we can specify the width.

Below you can see the Implementation of the above things below

import matplotlib.pyplot as plt

plt.plot([2, 4], [4, 4], zorder=2, linewidth=20,color="#7aebc1")
plt.plot([1, 5], [2, 6], zorder=1, linewidth=15,color=(.5,.95,1,1))
plt.show()
Colour change

Here above we have successfully implemented the changes. When using RBGA instead of using the pattern of (240,240,240,1) it is advised to use a value between 0 and 1.

Conclusion

In this article, we covered the Matplotlib Zorder. Besides that, we have also looked at its syntax and application. For better understanding, we looked at a couple of examples. We varied the syntax and looked at the output for each case. In the end, we can conclude that function Matplotlib Zorder is used to get the lines in a particular order as requested by the user.

I hope this article was able to clear all Doubts. But in case you have any unsolved queries feel free to write them below in the comment section. Done reading this why not read about Random Uniform Function next.

Subscribe
Notify of
guest
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Hamed Hajavi
Hamed Hajavi
3 years ago

Hello, very good.

What is the meaning of letter ‘z’ in the zorder parameter?

Last edited 3 years ago by Hamed Hajavi
Pratik Kinage
Admin
3 years ago
Reply to  Hamed Hajavi

The letter z demonstrates the order along the z-axis. The matplotlib has default x and y-axis. Suppose there is a condition where you have to make one graph closer to the observer than the other (above the other graph), here, you can use zorder to move it along the z-axis.
This method is widely used in CSS and is known as ‘z-index’ there. Have a look at it if you want to know more about more details.

Regards,
Pratik