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()
Here we can see a simple plot. 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()
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()
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
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()
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.
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()
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.
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.