Streamplot总是在前景中

时间:2017-03-03 19:26:33

标签: matplotlib z-order

使用Artist绘制streamplot时,第二个出现在前景中,尽管绘图是在Artist之前进行的:

enter image description here

pcolormesh相同的做法与预期相符:

enter image description here

这是两张图片的代码:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle

Y, X = np.mgrid[-3:3:100j, -3:3:100j]
U = -1 - X**2 + Y
V = 1 + X - Y**2
speed = np.sqrt(U*U + V*V)

fig0, ax0 = plt.subplots()
working = False
if working:
    strm = ax0.pcolormesh(X, Y, U, cmap=plt.cm.autumn)
else:
    strm = ax0.streamplot(X, Y, U, V, color=U, linewidth=2, cmap=plt.cm.autumn)
    fig0.colorbar(strm.lines)

#fig1, (ax1, ax2) = plt.subplots(ncols=2)
#ax1.streamplot(X, Y, U, V, density=[0.5, 1])
#
#lw = 5*speed / speed.max()
#ax2.streamplot(X, Y, U, V, density=0.6, color='k', linewidth=lw)

e = Rectangle(xy=(0,0), width=1, height=1)
e.set_facecolor([0.9,0.9,0.9])
ax0.add_artist(e)

plt.show()

我可以做什么,Artist覆盖streamplot

1 个答案:

答案 0 :(得分:2)

您可以更改艺术家的z顺序。对于您的示例,请尝试:

e = Rectangle(xy=(0,0), width=1, height=1, zorder=10)

根据a matplotlib example,默认的z顺序似乎是:

  • Patch / PatchCollection => 1
  • Line2D / LineCollection => 2
  • Text => 3

这可以解释为什么将在矩形(补丁)的顶部绘制streamplot(线)。