zorder

r2=ax2.add_patch(rect)

r2.set_zorder(-1) # <- This places the rectangle under the contour plot

plt.contourf(x_c,y_c,z)

plt.title('zorder of rect is set -1')

ax2=plt.subplot(1,2,2)

rect = patches.Rectangle(xy=(0,0), width=nx*0.5, height=ny*0.5, fc='gray', ec='#000000', fill=True)

# If above line is commmented out, then an error occurs as

# "re-use of an artist in more than one Axes which is not supported"

ax1=plt.subplot(1,2,1)

rect = patches.Rectangle(xy=(0,0), width=nx*0.5, height=ny*0.5, fc='gray', ec='#000000', fill=True)

r1=ax1.add_patch(rect)

plt.contourf(x_c,y_c,z)

plt.title('zorder of rect is not specified')

nx=4

ny=3

z=np.random.randn(ny,nx)

x_c=np.arange(nx)+0.5

y_c=np.arange(ny)+0.5

plt.ion()

# This is an example how to drwa rectangle etc beneath the contour or other plots

# by setting zorder -1.

from matplotlib import pyplot as plt

import matplotlib.patches as patches

import numpy as np

たまに,色を塗った矩形などを,pcolorやcontourfと一緒に,ただし図の順序として下に置きたいことがある.たとえば,欠損値を塗り方法の一つとして.これは矩形などにzorderを指定する事で実現できる.zorderは整数で,大きな値が上に置かれるようで,おそらくpcolorやconoturfの図のzorderは0になっている(未確認,図ではなくaxesかもしれない).

以下のサンプルプログラムは,重なりをzorderを指定する事で変えた添付の図を作る.

Sometimes we want to plot a put fill-colored rectangle beneath the plots of pcolor or contourf. This can be implemented by specifying zorder for the rectangle.

The following sample program produces the attached figure by specifying the zorder of the rectangle.

# This is an example how to drwa rectangle etc beneath the contour or other plots

# by setting zorder -1.

from matplotlib import pyplot as plt

import matplotlib.patches as patches

import numpy as np

nx=4

ny=3

z=np.random.randn(ny,nx)

x_c=np.arange(nx)+0.5

y_c=np.arange(ny)+0.5

plt.ion()

ax1=plt.subplot(1,2,1)

rect = patches.Rectangle(xy=(0,0), width=nx*0.5, height=ny*0.5, fc='gray', ec='#000000', fill=True)

r1=ax1.add_patch(rect)

plt.contourf(x_c,y_c,z)

plt.title('zorder of rect is not specified')

ax2=plt.subplot(1,2,2)

rect = patches.Rectangle(xy=(0,0), width=nx*0.5, height=ny*0.5, fc='gray', ec='#000000', fill=True)

# If above line is commmented out, then an error occurs as

# "re-use of an artist in more than one Axes which is not supported"

r2=ax2.add_patch(rect)

r2.set_zorder(-1) # <- This places the rectangle under the contour plot

plt.contourf(x_c,y_c,z)

plt.title('zorder of rect is set -1')