Zorder演示

轴的默认绘制顺序是补丁,线条,文本。 此顺序由zorder属性确定。 设置以下默认值

Artist Z-order
Patch / PatchCollection 1
Line2D / LineCollection 2
Text 3

您可以通过设置zorder来更改单个艺术家的顺序。任何单独的plot() 调用都可以为该特定项的zorder设置一个值。

在下面的第一个子图中,线条在散点图上方的补丁集合上方绘制,这是默认值。

在下面的子图中,顺序颠倒过来。

第二个图显示了如何控制各行的zorder。

  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. # Fixing random state for reproducibility
  4. np.random.seed(19680801)
  5. x = np.random.random(20)
  6. y = np.random.random(20)

分散的顶部的线

  1. plt.figure()
  2. plt.subplot(211)
  3. plt.plot(x, y, 'C3', lw=3)
  4. plt.scatter(x, y, s=120)
  5. plt.title('Lines on top of dots')
  6. # Scatter plot on top of lines
  7. plt.subplot(212)
  8. plt.plot(x, y, 'C3', zorder=1, lw=3)
  9. plt.scatter(x, y, s=120, zorder=2)
  10. plt.title('Dots on top of lines')
  11. plt.tight_layout()

Zorder演示

一个新的图像,带有单独订购的物品

  1. x = np.linspace(0, 2*np.pi, 100)
  2. plt.rcParams['lines.linewidth'] = 10
  3. plt.figure()
  4. plt.plot(x, np.sin(x), label='zorder=10', zorder=10) # on top
  5. plt.plot(x, np.sin(1.1*x), label='zorder=1', zorder=1) # bottom
  6. plt.plot(x, np.sin(1.2*x), label='zorder=3', zorder=3)
  7. plt.axhline(0, label='zorder=2', color='grey', zorder=2)
  8. plt.title('Custom order of elements')
  9. l = plt.legend(loc='upper right')
  10. l.set_zorder(20) # put the legend on top
  11. plt.show()

Zorder演示2

下载这个示例