Specifying the order of matplotlib layers

matplotlib twinx zorder
matplotlib zorder not working
matplotlib colors
matplotlib legend
matplotlib zorder 3d
matplotlib plot zorder
matplotlib legend zorder
matplotlib legend order

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:

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


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:

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

ZOrder Demo, In the subplot below, the order is reversed. The second figure shows how to control the zorder of individual lines. import matplotlib.pyplot as  Z-order. Patch / PatchCollection. 1. Line2D / LineCollection. 2. Text. 3. 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.


The layers are stacked from bottom to top in the same order of the corresponding calls to the plot function.

import matplotlib.pyplot as plt

lineWidth = 30
plt.figure()

plt.subplot(2, 1, 1)                               # upper plot
plt.plot([-1, 1], [-1, 1], lw=5*lineWidth, c='b')  # bottom blue
plt.plot([-1, 1], [-1, 1], lw=3*lineWidth, c='r')  # middle red
plt.plot([-1, 1], [-1, 1], lw=lineWidth, c='g')    # top green

plt.subplot(2, 1, 2)                               # lower plot
plt.plot([-1, 1], [-1, 1], lw=5*lineWidth, c='g')  # bottom green
plt.plot([-1, 1], [-1, 1], lw=3*lineWidth, c='r')  # middle red
plt.plot([-1, 1], [-1, 1], lw=lineWidth, c='b')    # top blue

plt.show()

It clearly emerges from the figure below that the plots are arranged according to the bottom first, top last rule.

Zorder Demo, 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  The scripting layer we use in this course is called pyplot. So that describes the matplotlib layer cake. There's a backend, which deals with actual drawing. A bunch of artists on top of the backend, which describe how data is arranged. And a scripting layer, which actually creates those artists and choreographs them all together.


While Tonechas is correct that the default order is back to front based on the order in which plots are called, it should be noted that using other plotting tools (scatter, errorbar, etc.) the default order is not as clear cut.

import matplotlib.pyplot as plt
import numpy as np

plt.errorbar(np.arange(0,10),np.arange(5,6,0.1),color='r',lw='3')
plt.plot(np.arange(0,10),np.arange(0,10),'b', lw=3)

plt.show()

Specifying the order of matplotlib layers – Icetutor, Specifying the order of matplotlib layers. python matplotlib plot layer. Suppose I run the following script: import matplotlib.pyplot as plt; lineWidth = 20; plt.figure()  The three layers from bottom to top are: backend, artist, and scripting.


Specifying the order of matplotlib layers, By default in matplotlib, if a line and a scatter plot are plotted in a same figure, the Specifying the order of matplotlib layers, stackoverflow  Doing the data labels is a little tricky, and we’re relying on the fact that Matplotlib’s bar graph has each of the bars, in order, as children of the axis object. Each of the rectangles has four corners, of course, and we’re looking for the upper right.


How to plot points in front of a line in matplotlib ?, Matplotlib is typically the first data visualization package that Python your plot objects see-through, allowing multiple layers to be seen at Order. Matplotlib's zorder property determines how close objects are to the foreground. To add text to a Matplotlib figure, just include annotation code specifying the  By default in matplotlib, if a line and a scatter plot are plotted in a same figure, the points are placed behind the line, illustration (no matter if the scatter() is called before plot()): How to plot points in front of a line in matplotlib ?


Simple Ways to Improve Your Matplotlib, IPython is built to work well with Matplotlib if you specify Matplotlib mode. to convey information in the visualization, in order to illustrate multidimensional data​. Layer Graph Objects Matplotlib allows users to layer multiple graphics on top of each other, which proves convenient when comparing results or setting baselines. Two useful properties should be utilized while layering: 1) alpha for controlling each component’s opacity and 2) zorder for moving objects to the foreground or background.