问题
I have a subplot that plots a line (x,y) and a particular point (xx,yy). I want to highligh (xx,yy), so I've plotted it with scatter
. However, even if I order it after the original plot, the new point still shows up behind the original line. How can I fix this? MWE below.
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")
回答1:
You can change which plots are displayed on top of each other with the argument zorder
. The matplotlib example shown here gives a brief explanation:
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.
A full example based on the code in the question, using python is shown below:
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()
Which gives:
来源:https://stackoverflow.com/questions/48106547/plot-ordering-layering-julia-pyplot