Search code examples
pythonmatplotlibmissing-data

Scatter Plot and missing values


I'm very new to python and I'm experimenting with matplotlib.pyplot. I'm plotting my data using a scatter plot. What I could see from the descriptive statistics all of my columns have 1/4 of missing values. So my question is how does a scatter plot treats missing values? does it ignore them (excluding them from the plot) or it replaces the values by 0? Thanks in advance.


Solution

  • If there are nan they are not plotted.

    Example:

    x = [1,2,3,4,5]
    y = [1,np.nan,np.nan, 3, 4]
    plt.scatter(x, y)
    plt.xlabel('x')
    plt.ylabel('y')
    plt.show()
    

    enter image description here

    On the contrary with y = [1,0,0,3,4]: enter image description here

    Of course you can replace the nan with 0 or other values. The 'how' depends the kind of your data. For list:

    import math
    y = [0 if math.isnan(e) else e for e in y]