python – Seaborn pairplot和NaN值

我试图理解为什么会失败,即使文档说:

dropna : boolean, optional
Drop missing values from the data before plotting.

from matplotlib import pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
sns.__version__
# '0.7.dev'
# generate an example DataFrame
a = pd.DataFrame(data={
    'a': np.random.normal(size=(100,)),
    'b': np.random.lognormal(size=(100,)),
    'c': np.random.exponential(size=(100,))})
sns.pairplot(a) # this works as expected
# snip
b = a.copy()
b.iloc[5,2] = np.nan # replace one value in col 'c' by a NaN
sns.pairplot(b) # this fails with error 
                # "AttributeError: max must be larger than min in range parameter."
                # in histogram(a, bins, range, normed, weights, density)"
> sns.pairplot(b, dropna=True) # same error as above

解决方法:

当你直接使用数据时,即

sns.pairplot(b) #Same as sns.pairplot(b, x_vars=['a','b','c'] , y_vars=['a','b','c'],dropna=True)

您正在绘制DataFrame中的所有列,然后确保所有列中的行数相同.

sns.pairplot(b, x_vars=['a','c'] , y_vars=['a','b','c'],dropna=True)

在这种情况下,它工作正常,但图表中将有一个微小的差异,以删除’NaN值’.

So, If you want to plot with the whole Data then :-

>必须使用“fillna()”替换空值,
>或者必须删除包含’nan values’的整行

b = b.drop(b.index[5])
sns.pairplot(b)

python  –  Seaborn pairplot和NaN值

上一篇:python 3 pandas和seaborn使用swarmplot斗争 – multiIndex


下一篇:python – seaborn FacetGrid图中timedata的设置