Matplotlib - Zorder Demo



The z-order demo (depth order) refers to the arrangement of objects along the z-axis (depth axis) in a three-dimensional space. Objects with a higher z-order value appear closer to the viewer, while those with a lower z-order value appear farther away.

Zorder Demo

We can see in the above image that a blue square and a red circle are plotted on the same axes. The circle has a higher Z-order, so it is drawn on top of the square, despite their overlapping coordinates.

Zorder Demo in Matplotlib

In Matplotlib, we can use the set_zorder() function to specify the drawing order (z-order) of a plot element. Elements with a higher zorder will be drawn on top of elements with a lower zorder.

This function is particularly useful when you have multiple plot elements, such as lines, markers, or patches, and you want certain elements to be visually prominent.

The set_zorder() Function

The set_zorder() function is used to control the stacking order of different plot elements. This function takes a numerical value representing the desired z-order and modifies the internal state of the Matplotlib artist on which it is called, setting the specified z-order for that artist.

Following is the syntax of the set_zorder() function in Matplotlib −

artist.set_zorder(z_order_value)

Where,

  • artist refers to the Matplotlib artist (e.g., line, patch, etc.) for which you want to specify the z-order.
  • z_order_value is a numerical value indicating the desired z-order.

Controlling Drawing Order with zorder

You can manage the layering of different elements in a plot in matplotlib by controlling drawing order with zorder. The zorder parameter assigns a numerical value to each element, determining the order in which they are drawn. Higher values mean an element is drawn on top, helping you to customize the visual hierarchy of plot.

Example

In the following example, we are creating a blue line and a red circle. We are then using the set_zorder() function to draw the line on top by specifying higher zorder value to it −

import matplotlib.pyplot as plt

# Creating a line and a circle
line, = plt.plot([0, 1], [0, 1], color='blue', linewidth=2, label='Line')
circle = plt.Circle((0.5, 0.5), 0.1, color='red', label='Circle')

# Setting different z-orders
line.set_zorder(2)
circle.set_zorder(1)

# Adding elements to the plot
plt.gca().add_patch(circle)
plt.legend()

# Displaying the plot
plt.title('Controlling Drawing Order with zorder')
plt.show()

Output

After executing the above code, we get the following output −

Controlling Drawing Order

Layering Bars and Text with zorder

You can control the stacking order of layering bars and text as well in matplotlib. You can manage their drawing order by assigning different zorder values to bars and text, making the plot more informative and visually appealing.

Example

In here, we are creating a green bar chart and a blue text annotation. We are then setting different zorder values to make the text appear on top of the bar −

import matplotlib.pyplot as plt

# Creating a bar and text
bar = plt.bar([1, 2, 3], [4, 7, 2], color='green', label='Bars')
text = plt.text(2, 5, 'Important', color='blue', fontsize=12)

# Setting different z-orders
bar[0].set_zorder(2)
text.set_zorder(1)
# Displaying the plot
plt.title('Layering Bars and Text with zorder')
plt.legend()
plt.show()

Output

Following is the output of the above code −

Layering Bars and Text

Ordering Scatter Plots with zorder

You can also control the ordering of multiple scatter plots with zorder on the same graph in matplotlib. This helps in comparing different sets of data points, ensuring that certain scatter plots appear in front of others for clarity in your overall visualization.

Example

Now, we are creating two scatter plots, and controlling their drawing order using the zorder parameter −

import matplotlib.pyplot as plt
import numpy as np

# Creating scatter plots with different z-orders
x = np.random.rand(10)
y = np.random.rand(10)
plt.scatter(x, y, s=100, color='orange', label='Scatter (Low Z-Order)', zorder=1)
plt.scatter(x, y + 0.1, s=100, color='blue', label='Scatter (High Z-Order)', zorder=2)

# Displaying the plot
plt.title('Ordering Scatter Plots with zorder')
plt.legend()
plt.show()

Output

Output of the above code is as follows −

Ordering Scatter Plots

Overlaying Multiple Plots with zorder

In Matplotlib, overlaying multiple plots with zorder allows you to super-impose different plots on the same graph. This helps you to showcase multiple datasets simultaneously, ensuring that specific plots are visually layered on top of others for a clear view.

Example

In the example below, we are overlaying three plots (Sin(x), Cos(x), and the sum of Sin(x) and Cos(x)) on the same graph with different zorder values, controlling their drawing order −

import matplotlib.pyplot as plt
import numpy as np

# Creating data for three different plots
x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)
y3 = np.sin(x) + np.cos(x)

# Plotting each dataset with specific z-order
plt.plot(x, y1, label='Sin(x)', color='blue', zorder=1)
plt.plot(x, y2, label='Cos(x)', color='orange', zorder=2)
plt.plot(x, y3, label='Sin(x) + Cos(x)', color='green', zorder=3)

# Displaying legend and title
plt.legend()
plt.title('Overlaying Multiple Plots with zorder')

# Displaying the plot
plt.show()

Output

The output obtained is as shown below −

Overlaying Multiple Plots
Advertisements