
- Matplotlib - Home
- Matplotlib - Introduction
- Matplotlib - Vs Seaborn
- Matplotlib - Environment Setup
- Matplotlib - Anaconda distribution
- Matplotlib - Jupyter Notebook
- Matplotlib - Pyplot API
- Matplotlib - Simple Plot
- Matplotlib - Saving Figures
- Matplotlib - Markers
- Matplotlib - Figures
- Matplotlib - Styles
- Matplotlib - Legends
- Matplotlib - Colors
- Matplotlib - Colormaps
- Matplotlib - Colormap Normalization
- Matplotlib - Choosing Colormaps
- Matplotlib - Colorbars
- Matplotlib - Working With Text
- Matplotlib - Text properties
- Matplotlib - Subplot Titles
- Matplotlib - Images
- Matplotlib - Image Masking
- Matplotlib - Annotations
- Matplotlib - Arrows
- Matplotlib - Fonts
- Matplotlib - Font Indexing
- Matplotlib - Font Properties
- Matplotlib - Scales
- Matplotlib - LaTeX
- Matplotlib - LaTeX Text Formatting in Annotations
- Matplotlib - PostScript
- Matplotlib - Mathematical Expressions
- Matplotlib - Animations
- Matplotlib - Celluloid Library
- Matplotlib - Blitting
- Matplotlib - Toolkits
- Matplotlib - Artists
- Matplotlib - Styling with Cycler
- Matplotlib - Paths
- Matplotlib - Path Effects
- Matplotlib - Transforms
- Matplotlib - Ticks and Tick Labels
- Matplotlib - Radian Ticks
- Matplotlib - Dateticks
- Matplotlib - Tick Formatters
- Matplotlib - Tick Locators
- Matplotlib - Basic Units
- Matplotlib - Autoscaling
- Matplotlib - Reverse Axes
- Matplotlib - Logarithmic Axes
- Matplotlib - Symlog
- Matplotlib - Unit Handling
- Matplotlib - Ellipse with Units
- Matplotlib - Spines
- Matplotlib - Axis Ranges
- Matplotlib - Axis Scales
- Matplotlib - Axis Ticks
- Matplotlib - Formatting Axes
- Matplotlib - Axes Class
- Matplotlib - Twin Axes
- Matplotlib - Figure Class
- Matplotlib - Multiplots
- Matplotlib - Grids
- Matplotlib - Object-oriented Interface
- Matplotlib - PyLab module
- Matplotlib - Subplots() Function
- Matplotlib - Subplot2grid() Function
- Matplotlib - Anchored Artists
- Matplotlib - Manual Contour
- Matplotlib - Coords Report
- Matplotlib - AGG filter
- Matplotlib - Ribbon Box
- Matplotlib - Fill Spiral
- Matplotlib - Findobj Demo
- Matplotlib - Hyperlinks
- Matplotlib - Image Thumbnail
- Matplotlib - Plotting with Keywords
- Matplotlib - Create Logo
- Matplotlib - Multipage PDF
- Matplotlib - Multiprocessing
- Matplotlib - Print Stdout
- Matplotlib - Compound Path
- Matplotlib - Sankey Class
- Matplotlib - MRI with EEG
- Matplotlib - Stylesheets
- Matplotlib - Background Colors
- Matplotlib - Basemap
- Matplotlib - Event Handling
- Matplotlib - Close Event
- Matplotlib - Mouse Move
- Matplotlib - Click Events
- Matplotlib - Scroll Event
- Matplotlib - Keypress Event
- Matplotlib - Pick Event
- Matplotlib - Looking Glass
- Matplotlib - Path Editor
- Matplotlib - Poly Editor
- Matplotlib - Timers
- Matplotlib - Viewlims
- Matplotlib - Zoom Window
- Matplotlib Widgets
- Matplotlib - Cursor Widget
- Matplotlib - Annotated Cursor
- Matplotlib - Buttons Widget
- Matplotlib - Check Buttons
- Matplotlib - Lasso Selector
- Matplotlib - Menu Widget
- Matplotlib - Mouse Cursor
- Matplotlib - Multicursor
- Matplotlib - Polygon Selector
- Matplotlib - Radio Buttons
- Matplotlib - RangeSlider
- Matplotlib - Rectangle Selector
- Matplotlib - Ellipse Selector
- Matplotlib - Slider Widget
- Matplotlib - Span Selector
- Matplotlib - Textbox
- Matplotlib Plotting
- Matplotlib - Line Plots
- Matplotlib - Area Plots
- Matplotlib - Bar Graphs
- Matplotlib - Histogram
- Matplotlib - Pie Chart
- Matplotlib - Scatter Plot
- Matplotlib - Box Plot
- Matplotlib - Arrow Demo
- Matplotlib - Fancy Boxes
- Matplotlib - Zorder Demo
- Matplotlib - Hatch Demo
- Matplotlib - Mmh Donuts
- Matplotlib - Ellipse Demo
- Matplotlib - Bezier Curve
- Matplotlib - Bubble Plots
- Matplotlib - Stacked Plots
- Matplotlib - Table Charts
- Matplotlib - Polar Charts
- Matplotlib - Hexagonal bin Plots
- Matplotlib - Violin Plot
- Matplotlib - Event Plot
- Matplotlib - Heatmap
- Matplotlib - Stairs Plots
- Matplotlib - Errorbar
- Matplotlib - Hinton Diagram
- Matplotlib - Contour Plot
- Matplotlib - Wireframe Plots
- Matplotlib - Surface Plots
- Matplotlib - Triangulations
- Matplotlib - Stream plot
- Matplotlib - Ishikawa Diagram
- Matplotlib - 3D Plotting
- Matplotlib - 3D Lines
- Matplotlib - 3D Scatter Plots
- Matplotlib - 3D Contour Plot
- Matplotlib - 3D Bar Plots
- Matplotlib - 3D Wireframe Plot
- Matplotlib - 3D Surface Plot
- Matplotlib - 3D Vignettes
- Matplotlib - 3D Volumes
- Matplotlib - 3D Voxels
- Matplotlib - Time Plots and Signals
- Matplotlib - Filled Plots
- Matplotlib - Step Plots
- Matplotlib - XKCD Style
- Matplotlib - Quiver Plot
- Matplotlib - Stem Plots
- Matplotlib - Visualizing Vectors
- Matplotlib - Audio Visualization
- Matplotlib - Audio Processing
- Matplotlib Useful Resources
- Matplotlib - Quick Guide
- Matplotlib - Cheatsheet
- Matplotlib - Useful Resources
- Matplotlib - Discussion
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.

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 −

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 −

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 −

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 −
