Matplotlib Has Numerous Hidden Gems. Here's One of Them.
A cool trick to improve Matplotlib plots.
One of the best yet underrated and underutilized potentials of matplotlib is customizability. Here's a pretty interesting thing you can do with it.
By default, matplotlib renders different types of elements (also called artists), like plots, legend, texts, etc., in a specific order.
But this ordering may not be desirable in all cases, especially when there are overlapping elements in a plot, or the default rendering is hiding some crucial details.
With the zorder
parameter, you can control this rendering order. As a result, plots with higher zorder
value appear closer to the viewer and are drawn on top of artists with lower zorder
values.
Lastly, in the above demonstration, if we specify zorder=0
for the line plot, we notice that it goes behind the grid lines.
You can find more details about zorder
here: Matplotlib docs.
👉 Tell me you liked this post by leaving a heart react 🤍.
👉 If you love reading this newsletter, feel free to share it with friends!
Find the code for my tips here: GitHub.
I like to explore, experiment and write about data science concepts and tools. You can read my articles on Medium. Also, you can connect with me on LinkedIn and Twitter.