ValueError: “color kwarg must have one color per dataset”

前端 未结 2 1261
故里飘歌
故里飘歌 2020-12-07 04:11

I have a dataset regarding some used cars from eBay, which I tried to plot after I edited the dataset as follows:

import pandas as pd

df = pd.read_csv(\"./a         


        
相关标签:
2条回答
  • 2020-12-07 04:49

    The problem comes from the fact that pairplot only accepts some pandas types in the PairGrid: float or int, but not Object or Int64 for exemple (at least for some versions of matplotlib and/or seaborn: 3.0.3 and 0.9.0 respectivly generates that error).

    Using .astype('float') to modify the concerned series before plotting in the following exemple solves the problem as a.One is set to Int64 and a.Two is initially an Object type:

    a = pd.DataFrame()
    a['One'] = [1, 3, 3, 2, 1]
    a['One']=a['One'].astype('Int64')
    a['Two'] = ['yes', 'yes', 'no', 'yes', 'no']
    a['Two'] = np.where(a['Two'] == 'yes', 1, a['Two'])
    a['Two'] = np.where(a['Two'] == 'no', 0, a['Two'])
    a['One']=a['One'].astype('int')
    a['Two']=a['Two'].astype('int')
    sns.pairplot(a)
    plt.show()
    

    Remark that if you have some NaN in your dataframe, float is the only option as Int64 accepts missing values but not the Int type.

    0 讨论(0)
  • 2020-12-07 04:58

    Following your comment, an example snippet, which hopefully can help you. Maybe the issue is with IPython? Unfortunately, I do not know. Having your dataset available would definitely help.

    import matplotlib.pyplot as plt
    import numpy as np
    import pandas as pd
    import seaborn as sns
    
    a = pd.DataFrame()
    a['One'] = [1, 3, 3, 2, 1]
    a['Two'] = ['ja', 'ja', 'nein', 'ja', 'nein']
    a['Two'] = np.where(a['Two'] == 'ja', 1, a['Two'])
    a['Two'] = np.where(a['Two'] == 'nein', 0, a['Two'])
    a = a[a['Two'].notnull()]
    print(a)
    sns.pairplot(a)
    plt.show()
    

    This prints

       One Two
    0    1   1
    1    3   1
    2    3   0
    3    2   1
    4    1   0
    

    and displays

    0 讨论(0)
提交回复
热议问题