我有一个绘制线 (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()
这给出:
关于matplotlib - 绘图排序/分层 Julia pyplot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48106547/