Categories
computing matplotlib python

zorder: Ordering components of a plot in matplotlib

Matplotlib has its own order when it places different components of a plot. For instance, if there is a line plot and a scatter plot, it will always put the line plot on top of the scatter plot no matter what is the order in which the plots commands are given in the code. In order to control the order of the plot components one needs to use the kwarg zorder” which takes integer values. A higher value means the component will be placed above one with a lower value. Here is an example from the matplotlib documentation


from pylab import *
x = rand(20); y = rand(20)

subplot(211)
plot(x, y, 'r', lw=3)
scatter(x,y,s=120)

subplot(212)
plot(x, y, 'r', zorder=1, lw=3)
scatter(x,y,s=120, zorder=2)

show

which produces the following plot

Note that in the code the scatter plot has zorder=2 in the second plot which makes it fall above the line plot contrary to the first subplot.

Leave a comment