2

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")
jjjjjj
  • 1,152
  • 1
  • 14
  • 30
  • I can reproduce the issue. As a side comment, you don't need to do `ax[:plot](x,y)` if there is only one axes and figure in the analysis. Just do `plot(x,y)` and plots will be added to the current axes until you call `subplots()` again. At least that is what I remember from matplotlib. – juliohm Jan 05 '18 at 06:11

1 Answers1

2

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:

enter image description here

DavidG
  • 24,279
  • 14
  • 89
  • 82
  • It worked for me in Julia too, thanks! BTW, I've added my edit to the code to your answer above. – jjjjjj Jan 06 '18 at 19:25