情节 ordering/layering 朱莉娅 pyplot

plot ordering/layering julia pyplot

我有一个绘制线 (x,y) 和特定点 (xx,yy) 的子图。我想突出显示 (xx,yy),所以我用 scatter 绘制了它。然而,即使我在原图之后排序,新点仍然出现在原线的后面。我怎样才能解决这个问题? MWE 下面。

x = 1:10
y = 1:10
xx = 5
yy = 5
fig, ax = subplots()
ax[:plot](x,y)
ax[:scatter](xx,yy, color="red", label="h_star", s=100)
legend()
xlabel("x")
ylabel("y")
title("test")
grid("on")

您可以使用参数 zorder 更改显示在彼此之上的图。显示的matplotlib示例here给出了简要说明:

The default drawing order for axes is patches, lines, text. This order is determined by the zorder attribute. The following defaults are set

Artist                      Z-order

Patch / PatchCollection      1

Line2D / LineCollection      2

Text                         3

You can change the order for individual artists by setting the zorder. Any individual plot() call can set a value for the zorder of that particular item.

基于问题中的代码的完整示例,使用 python 如下所示:

import matplotlib.pyplot as plt

x = range(1,10)
y = range(1,10)
xx = 5
yy = 5
fig, ax = plt.subplots()
ax.plot(x,y)

# could set zorder very high, say 10, to "make sure" it will be on the top
ax.scatter(xx,yy, color="red", label="h_star", s=100, zorder=3)

plt.legend()
plt.xlabel("x")
plt.ylabel("y")
plt.title("test")
plt.grid("on")

plt.show()

给出: