Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specifying the order of matplotlib layers

Suppose I run the following script:

import matplotlib.pyplot as plt  lineWidth = 20 plt.figure() plt.plot([0,0],[-1,1], lw=lineWidth, c='b') plt.plot([-1,1],[-1,1], lw=lineWidth, c='r') plt.plot([-1,1],[1,-1], lw=lineWidth, c='g') plt.show() 

This produces the following:

enter image description here

How can I specify the top-to-bottom order of the layers instead of having Python pick for me?

like image 977
Forklift17 Avatar asked May 16 '16 04:05

Forklift17


People also ask

How do I change the order of my plot in matplotlib?

You can change the order for individual artists by setting the zorder. Any individual plot() call can set a value for the zorder of that particular item. In the fist subplot below, the lines are drawn above the patch collection from the scatter, which is the default. In the subplot below, the order is reversed.

What are the 3 layers of matplotlib architecture?

As shown in Figure 1, there are three main layers in matplotlib architecture. From top to bottom, they are Scripting layer ( matplotlib. pyplot module), Artist layer ( matplotlib. artist module), and Backend layer ( matplotlib.

What is Z order python?

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.

Which layer of matplotlib is used for plotting?

Scripting Layer This is why many Matplotlib tutorials prefer to introduce from this layer. It is the easiest part to start with and use, you add up objects — e.g. line, text, rectangle- on top of the figure. Scripting layer plotting is sometimes also called procedural plotting.


1 Answers

I don't know why zorder has that behavior and it's likely that might be a bug or, at the very least, a badly documented feature. It might be because there are already automatic references to zorder when you build a plot (like grid, axis, and so on...) and when you try to specify the zorder for elements you are somehow overlapping them. This is hypothetical in any case.

For you to solve your problem just make the differences in zorder exaggerated. For instance instead of 0,1,2, make it 0,5,10:

import matplotlib.pyplot as plt  lineWidth = 20 plt.figure() plt.plot([0,0],[-1,1], lw=lineWidth, c='b',zorder=10) plt.plot([-1,1],[-1,1], lw=lineWidth, c='r',zorder=5) plt.plot([-1,1],[1,-1], lw=lineWidth, c='g',zorder=0) plt.show() 

, which results in this:

Handling zorder in mataplotlib

For this plot I specified the opposite order shown in your question.

like image 112
armatita Avatar answered Sep 29 '22 10:09

armatita