如何在背景中放置matplotlib艺术家以覆盖顶部的情节?

seb*_*ebo 2 matplotlib

我知道我遗漏了关于matplotlib如何组织数字和情节的非常基本的东西,但我搜索了没有结果的文档.我已经将我的问题缩小到简单的东西,希望能帮助我更好地理解matplotlib.

鉴于以下代码:

x_coords = [1,2,3]
y_coords = [2,3,4]
labels = ['A','B','C']
plt.scatter(x_coords, y_coords, marker = 'o')
for l, x, y in zip(labels, x_coords, y_coords):
    plt.annotate(l, xy=(x,y), xytext=(-10,5), textcoords='offset points')

circle = plt.Circle((2,3), 1.5, color='w', ec='k')
fig = plt.gcf()
fig.gca().add_artist(circle)

plt.show()
Run Code Online (Sandbox Code Playgroud)

圆圈绘制在标记和绘制点标签之间的图层上.如何控制绘制这些元素的图层?

以下是可视参考的绘制图像:

简单的情节

sod*_*odd 5

首先,circle你的代码中不是a Figure,它是a Artist,更具体地说是a Patch.在matplotlib中,a FigureArtist包含其他元素的顶级,因此您的标题有点误导.

其次,你可以通过指定它的zorderkwarg 将圆放在其他艺术家的下方:

circle = plt.Circle((2,3), 1.5, color='w', ec='k', zorder=0)
Run Code Online (Sandbox Code Playgroud)

最低的艺术家在底层zorder绘制,最高的艺术家在顶部绘制.

在此输入图像描述