pingul pingul - 2 months ago 16
Python Question

Plot is obscuring other plot in matplotlib

I have one scatter plot and one line plot in the same figure. I create it using

fig, ax = plt.subplots()
scat = plt.scatter([0], [0], color = 'r')
line, = ax.plot(pos_data.x, pos_data.y, color = "k")


The line plot is currently obscuring the scatter plot, but I would like it the other way around (i.e. it should look like the line plot is being rendered first, and the scatter plot on top of that one).

Is this possible?

Answer

Try changing the Z order

 plt.plot(x, y, 'r', zorder=1, lw=3)
 plt.scatter(x, y, s=120, zorder=2)

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.