Search code examples
pythonmatplotlibscatter-plotalpha

How to make pyplot scatter markers opaque?


I want to make a plot of lines with opaque scatter markers superimposed on them at certain points. But the lines keep shining through markers, even with alpha=1:

import matplotlib.pyplot as plt
plt.close('all')
plt.plot([0, 2], [0, 0], color='black')
plt.scatter([1], [0], color='red', alpha=1)
plt.savefig('/tmp/foo.png')

red marker not opaque at all

How to make the red marker really opaque, i.e. make the black line completely invisible beneath it?


Solution

  • Your problem is the z-order of the elements. Default, lines will be drawn on top of markers. Use plt.scatter(..., zorder=3) to force the markers on top.

    import matplotlib.pyplot as plt
    plt.plot([0, 2], [0, 0], color='black')
    plt.scatter([1], [0], color='red', alpha=1, zorder=3)
    plt.show()
    

    example plot