pythonmatplotlibscatter-plot

Prevent axes from cutting off dots in matplotlib scatter plots


import matplotlib.pyplot as plt


x = [1, 2, 3, 4]
y = [0, 1, 7, 2]

plt.scatter(x, y, color='red')
plt.title('number of iterations')
plt.xlim([1, 4])
plt.ylim([1, 8])

If one was to plot this data, the dots on the axes are partially cut off. Is there a way to prevent this (i.e. can the dots be plotted on top of the axes)?


Solution

  • Setting the clip_on attribute to False allows you to go beyond the axes, but by default the axes will be on top. For example, the script

    x = [1, 2, 3, 4]
    y = [0, 1, 7, 2]
    
    plt.scatter(x, y, color="red", clip_on=False)
    plt.title('number of iterations')
    plt.xlim([1, 4])
    plt.ylim([1, 8])
    

    Yields the following.

    enter image description here

    Note that the axes "cut through" the dots. If you want the dots to go on top of the axes/labels, you need to change the default zorder. For example, the script

    x = [1, 2, 3, 4]
    y = [0, 1, 7, 2]
    
    plt.scatter(x, y, color="red", clip_on=False, zorder = 10)
    plt.title('number of iterations')
    plt.xlim([1, 4])
    plt.ylim([1, 8])
    

    yields

    enter image description here

    Note: any zorder value 3 or greater will work here.