Adjusting Matplotlib Drawing Order

MatplotlibMatplotlibBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this tutorial, we will learn about the drawing order of artists in Matplotlib and how to adjust the order using the zorder attribute. We will also explore how to change the order for individual artists and the default value of zorder for different types of artists.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlottingDataGroup(["`Plotting Data`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlotCustomizationGroup(["`Plot Customization`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/AdvancedTopicsGroup(["`Advanced Topics`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("`Understanding Figures and Axes`") matplotlib/PlottingDataGroup -.-> matplotlib/line_plots("`Line Plots`") matplotlib/PlottingDataGroup -.-> matplotlib/scatter_plots("`Scatter Plots`") matplotlib/PlotCustomizationGroup -.-> matplotlib/line_styles_colors("`Customizing Line Styles and Colors`") matplotlib/PlotCustomizationGroup -.-> matplotlib/titles_labels("`Adding Titles and Labels`") matplotlib/PlotCustomizationGroup -.-> matplotlib/legend_config("`Legend Configuration`") matplotlib/PlotCustomizationGroup -.-> matplotlib/grid_config("`Grid Configuration`") matplotlib/AdvancedTopicsGroup -.-> matplotlib/matplotlib_config("`Customizing Matplotlib Configurations`") python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") subgraph Lab Skills python/comments -.-> lab-49040{{"`Adjusting Matplotlib Drawing Order`"}} matplotlib/importing_matplotlib -.-> lab-49040{{"`Adjusting Matplotlib Drawing Order`"}} matplotlib/figures_axes -.-> lab-49040{{"`Adjusting Matplotlib Drawing Order`"}} matplotlib/line_plots -.-> lab-49040{{"`Adjusting Matplotlib Drawing Order`"}} matplotlib/scatter_plots -.-> lab-49040{{"`Adjusting Matplotlib Drawing Order`"}} matplotlib/line_styles_colors -.-> lab-49040{{"`Adjusting Matplotlib Drawing Order`"}} matplotlib/titles_labels -.-> lab-49040{{"`Adjusting Matplotlib Drawing Order`"}} matplotlib/legend_config -.-> lab-49040{{"`Adjusting Matplotlib Drawing Order`"}} matplotlib/grid_config -.-> lab-49040{{"`Adjusting Matplotlib Drawing Order`"}} matplotlib/matplotlib_config -.-> lab-49040{{"`Adjusting Matplotlib Drawing Order`"}} python/booleans -.-> lab-49040{{"`Adjusting Matplotlib Drawing Order`"}} python/lists -.-> lab-49040{{"`Adjusting Matplotlib Drawing Order`"}} python/tuples -.-> lab-49040{{"`Adjusting Matplotlib Drawing Order`"}} python/importing_modules -.-> lab-49040{{"`Adjusting Matplotlib Drawing Order`"}} python/numerical_computing -.-> lab-49040{{"`Adjusting Matplotlib Drawing Order`"}} python/data_visualization -.-> lab-49040{{"`Adjusting Matplotlib Drawing Order`"}} end

Understanding Zorder

The zorder attribute in Matplotlib is a floating point number that determines the drawing order of artists. Artists with higher zorder are drawn on top of those with lower zorder. The default value of zorder depends on the type of the artist. For example, images have a default zorder of 0, while patches have a default zorder of 1.

Changing Zorder

To change the drawing order of artists, we can set their zorder attribute explicitly using the zorder parameter when creating the artist. For example, we can move dots on top of lines in a scatter plot by setting the zorder of the dots to a value higher than the zorder of the line.

import matplotlib.pyplot as plt
import numpy as np

r = np.linspace(0.3, 1, 30)
theta = np.linspace(0, 4*np.pi, 30)
x = r * np.sin(theta)
y = r * np.cos(theta)

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(6, 3.2))

ax1.plot(x, y, 'C3', lw=3)
ax1.scatter(x, y, s=120)
ax1.set_title('Lines on top of dots')

ax2.plot(x, y, 'C3', lw=3)
ax2.scatter(x, y, s=120, zorder=2.5)  ## move dots on top of line
ax2.set_title('Dots on top of lines')

plt.tight_layout()
plt.show()

Setting Zorder for Ticks and Grid Lines

We can use the set_axisbelow() method or the axes.axisbelow parameter to set the zorder of ticks and grid lines.

ax = plt.axes()
ax.plot([1, 2, 3], [2, 4, 3])
ax.set_axisbelow(True)
ax.yaxis.grid(color='gray', linestyle='dashed')

Custom Order of Elements

We can also set the zorder of elements in a custom order. For example, we can set the zorder of a legend to be between two lines.

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 7.5, 100)
plt.rcParams['lines.linewidth'] = 5
plt.figure()
plt.plot(x, np.sin(x), label='zorder=2', zorder=2)  ## bottom
plt.plot(x, np.sin(x+0.5), label='zorder=3',  zorder=3)
plt.axhline(0, label='zorder=2.5', color='lightgrey', zorder=2.5)
plt.title('Custom order of elements')
l = plt.legend(loc='upper right')
l.set_zorder(2.5)  ## legend between blue and orange line
plt.show()

Summary

In this tutorial, we learned about the zorder attribute in Matplotlib and how to change the drawing order of artists. We also explored how to set the zorder for ticks and grid lines and create a custom order of elements. Understanding zorder is essential when creating complex visualizations with overlapping elements.

Other Matplotlib Tutorials you may like