Python 因颜色参数导致Seaborn异常
我试图画出这样的东西: 对于一个可复制的示例,让我提供一些玩具数据:Python 因颜色参数导致Seaborn异常,python,pandas,seaborn,Python,Pandas,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
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
---------------------------------------------------------------------------
ValueError回溯(最近一次调用上次)
在()
1个bins=np.linspace(toy_data.Principal.min(),toy_data.Principal.max(),10)
2 g=sns.FaceGrid(玩具数据,col=“Gender”,hue=“loan\u status”,palete=“Set1”,col\u wrap=2)
---->3 g.map(plt.hist,“委托人”,箱子=箱子,ec=“k”)
4.
5 g.轴[-1]。图例()
C:\ProgramData\Anaconda3\lib\site packages\seaborn\axisgrid.py在映射中(self、func、*args、**kwargs)
741
742#画出情节
-->743自切面图(func、ax、图参数、kwargs)
744
745#完成注释和布局
C:\ProgramData\Anaconda3\lib\site packages\seaborn\axisgrid.py in\u facet\u plot(self、func、ax、plot\u args、plot\u kwargs)
825
826——画出情节
-->827函数(*绘图参数,**绘图参数)
828
829——整理支持信息
C:\ProgramData\Anaconda3\lib\site packages\matplotlib\pyplot.py in hist(x、箱子、范围、密度、重量、累积、底部、histtype、对齐、方向、宽度、日志、颜色、标签、堆叠、标准化、保持、数据,**kwargs)
3130 histtype=histtype,align=align,orientation=orientation,
3131 rwidth=rwidth,log=log,color=color,label=label,
->3132堆叠=堆叠,规范=规范,数据=数据,**kwargs)
3133最后:
3134斧头_保持=洗旧
C:\ProgramData\Anaconda3\lib\site packages\matplotlib\\uuuuu init\uuuuuuuu.py在内部(ax,*args,**kwargs)
1853年的今天,“Matplotlib列表!”%(标签名称,函数名称),
1854运行时警告,堆栈级别=2)
->1855返回函数(ax,*args,**kwargs)
1856
1857内部.\uuuuu文档\uuuuu=\u添加数据\u文档(内部.\uuuuu文档,
hist中的C:\ProgramData\Anaconda3\lib\site packages\matplotlib\axes\\u axes.py(***解析参数失败***)
6502 color=mcolors.to_rgba_数组(颜色)
6503如果透镜(颜色)!=nx:
->6504 raise VALUERROR(“颜色kwarg必须为每个数据集提供一种颜色”)
6505
6506#如果未明确或通过范围指定存储箱,
ValueError:color kwarg每个数据集必须有一种颜色
我不确定为什么会收到此错误消息,以及我应该如何更正它。以下是我们得到的信息:
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的版本。看起来它们不兼容。我无法用此数据集重现您的错误。请为我们提供易于导入的数据。例如,
toy_data.to_json()的输出
我按照你的要求做了。请看我上面编辑的帖子。你说得对。我更新了两个软件包,问题解决了。谢谢!