首页 > 解决方案 > 由于颜色参数,Seaborn 异常

问题描述

我正在尝试绘制一些看起来像这样的东西:

在此处输入图像描述

对于一个可重现的例子,让我提供一些玩具数据:

    toy_data

    Principal Gender loan_status
    0           1      0     PAIDOFF
    1           1      1     PAIDOFF
    2           1      0     PAIDOFF
    3           1      1     PAIDOFF
    4           1      0     PAIDOFF
    341  0.714286      0  COLLECTION
    342         1      0  COLLECTION
    343  0.714286      0  COLLECTION
    344         1      0  COLLECTION
    345         1      0  COLLECTION

或者:

'{"Principal":{"0":1.0,"1":1.0,"2":1.0,"3":1.0,"4":1.0,"341":0.7142857143,"342":1.0,"343":0.7142857143,"344":1.0,"345":1.0},"Gender":{"0":0.0,"1":1.0,"2":0.0,"3":1.0,"4":0.0,"341":0.0,"342":0.0,"343":0.0,"344":0.0,"345":0.0},"loan_status":{"0":"PAIDOFF","1":"PAIDOFF","2":"PAIDOFF","3":"PAIDOFF","4":"PAIDOFF","341":"COLLECTION","342":"COLLECTION","343":"COLLECTION","344":"COLLECTION","345":"COLLECTION"}}'

我的代码如下:

bins=np.linspace(toy_data.Principal.min(), toy_data.Principal.max(), 10)
g = sns.FacetGrid(toy_data, col="Gender", hue="loan_status", palette="Set1", col_wrap=2)
g.map(plt.hist, 'Principal', bins=bins, ec="k")
g.axes[-1].legend()
plt.show()

它返回以下错误消息:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-135-ef89846d23ff> in <module>()
      1 bins=np.linspace(toy_data.Principal.min(), toy_data.Principal.max(), 10)
      2 g = sns.FacetGrid(toy_data, col="Gender", hue="loan_status", palette="Set1", col_wrap=2)
----> 3 g.map(plt.hist, 'Principal', bins=bins, ec="k")
      4 
      5 g.axes[-1].legend()

C:\ProgramData\Anaconda3\lib\site-packages\seaborn\axisgrid.py in map(self, func, *args, **kwargs)
    741 
    742             # Draw the plot
--> 743             self._facet_plot(func, ax, plot_args, kwargs)
    744 
    745         # Finalize the annotations and layout

C:\ProgramData\Anaconda3\lib\site-packages\seaborn\axisgrid.py in _facet_plot(self, func, ax, plot_args, plot_kwargs)
    825 
    826         # Draw the plot
--> 827         func(*plot_args, **plot_kwargs)
    828 
    829         # Sort out the supporting information

C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\pyplot.py in hist(x, bins, range, density, weights, cumulative, bottom, histtype, align, orientation, rwidth, log, color, label, stacked, normed, hold, data, **kwargs)
   3130                       histtype=histtype, align=align, orientation=orientation,
   3131                       rwidth=rwidth, log=log, color=color, label=label,
-> 3132                       stacked=stacked, normed=normed, data=data, **kwargs)
   3133     finally:
   3134         ax._hold = washold

C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\__init__.py in inner(ax, *args, **kwargs)
   1853                         "the Matplotlib list!)" % (label_namer, func.__name__),
   1854                         RuntimeWarning, stacklevel=2)
-> 1855             return func(ax, *args, **kwargs)
   1856 
   1857         inner.__doc__ = _add_data_doc(inner.__doc__,

C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\axes\_axes.py in hist(***failed resolving arguments***)
   6502             color = mcolors.to_rgba_array(color)
   6503             if len(color) != nx:
-> 6504                 raise ValueError("color kwarg must have one color per dataset")
   6505 
   6506         # If bins are not specified either explicitly or via range,

ValueError: color kwarg must have one color per dataset

我不知道为什么我会收到此错误消息以及我应该做些什么来纠正它。

标签: pythonpandasseaborn

解决方案


这是我们得到的:

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

toy_data = pd.read_json('{"Principal":{"0":1.0,"1":1.0,"2":1.0,"3":1.0,"4":1.0,"341":0.7142857143,"342":1.0,"343":0.7142857143,"344":1.0,"345":1.0},"Gender":{"0":0.0,"1":1.0,"2":0.0,"3":1.0,"4":0.0,"341":0.0,"342":0.0,"343":0.0,"344":0.0,"345":0.0},"loan_status":{"0":"PAIDOFF","1":"PAIDOFF","2":"PAIDOFF","3":"PAIDOFF","4":"PAIDOFF","341":"COLLECTION","342":"COLLECTION","343":"COLLECTION","344":"COLLECTION","345":"COLLECTION"}}')

bins=np.linspace(toy_data.Principal.min(), toy_data.Principal.max(), 10)
g = sns.FacetGrid(toy_data, col="Gender", hue="loan_status", palette="Set1", col_wrap=2, height=4)
g.map(plt.hist, 'Principal', bins=bins, ec="k")
g.axes[-1].legend()
plt.show()

结果


我没有收到任何错误。告诉我们您使用的是什么版本的 matplotlib 和 seaborn。看起来他们是不相容的。


推荐阅读