What to do with missing values when plotting with seaborn?

前端 未结 3 885
别跟我提以往
别跟我提以往 2021-02-07 11:19

I replaced the missing values with NaN using lambda following function:

data = data.applymap(lambda x: np.nan if isinstance(x, basestring) and x.isspace() else x)<

3条回答
  •  执念已碎
    2021-02-07 11:35

    This is a known issue with matplotlib/pylab histograms!

    See e.g. https://github.com/matplotlib/matplotlib/issues/6483

    where various workarounds are suggested, two favourites (for example from https://stackoverflow.com/a/19090183/1021819) being:

    import numpy as np
    nbins=100
    A=data['alcconsumption']
    Anan=A[~np.isnan(A)] # Remove the NaNs
    
    seaborn.distplot(Anan,hist=True,bins=nbins)
    

    Alternatively, specify bin edges (in this case by anyway making use of Anan...):

    Amin=min(Anan)
    Amax=max(Anan)
    seaborn.distplot(A,hist=True,bins=np.linspace(Amin,Amax,nbins))
    

提交回复
热议问题