我试图在Python Data Science Essential一书中运行一个例子。但是,当我运行它时,它出现了错误。实际上,我刚开始学习python。所以,我觉得很难解决这些错误。请帮我。 这是代码:
In:
import pandas as pd
import numpy as np
In: colors = list()
In: palette = {0: "red", 1: "green", 2: "blue"}
In:
for c in np.nditer(iris.target): colors.append(palette[int(c)])
# using the palette dictionary, we convert
# each numeric class into a color string
In: dataframe = pd.DataFrame(iris.data,
columns=iris.feature_names)
In: scatterplot = pd.scatter_matrix(dataframe, alpha=0.3,
figsize=(10, 10), diagonal='hist', color=colors, marker='o',
grid=True)
这是错误:
ValueError Traceback(最近一次调用 最后)in() 1 scatterplot = pd.scatter_matrix(dataframe,alpha = 0.3, ----> 2 figsize =(10,10),diagonal ='hist',color = colors,marker ='o',grid = True)
/Users/leeivan/anaconda/lib/python2.7/site-packages/pandas/tools/plotting.py 在scatter_matrix中(frame,alpha,figsize,ax,grid,diagonal,marker, density_kwds,hist_kwds,range_padding,** kwds) 378 379 ax.scatter(df [b] [common],df [a] [common], - > 380 marker = marker,alpha = alpha,** kwds) 381 382 ax.set_xlim(borders_list [j])
/Users/leeivan/anaconda/lib/python2.7/site-packages/matplotlib/的初始化 pyc文件 在内部(ax,* args,** kwargs)1817年 warnings.warn(msg%(label_namer,func。 name ),1818
RuntimeWarning,stacklevel = 2) - > 1819 return func(ax,* args,** kwargs)1820 pre_doc = inner。 doc 1821如果pre_doc为None:/Users/leeivan/anaconda/lib/python2.7/site-packages/matplotlib/axes/_axes.pyc 在散射中(self,x,y,s,c,marker,cmap,norm,vmin,vmax,alpha, linewidths,verts,edgecolors,** kwargs)3787
facecolors = co 3788如果c不是None: - > 3789引发ValueError(“供应'c'kwarg或'颜色'kwarg”3790“但不是 都;他们不同但“3791” 它们的功能重叠。“)ValueError:提供'c'kwarg或'color'kwarg但不是两者兼而有之;他们 不同但功能重叠。
答案 0 :(得分:6)
我在jupyter和python 3.5中测试了下面的代码,它可以工作。
import pandas as pd
import numpy as np
from sklearn.datasets import load_iris
%matplotlib inline
iris = load_iris()
colors = list()
palette = {0: "red", 1: "green", 2: "blue"}
for c in np.nditer(iris.target): colors.append(palette[int(c)])
# using the palette dictionary, we convert
# each numeric class into a color string
dataframe = pd.DataFrame(iris.data,
columns=iris.feature_names)
scatterplot = pd.scatter_matrix(dataframe, alpha=0.3,
figsize=(10, 10), diagonal='hist', c=colors, marker='o', grid=True)
显然,参数color
正在生成错误,而c
正在运行。
另一方面,它可能是matplotlib中的一个错误。
这是我发现的,看着熊猫的功能:
def scatter_matrix(frame, alpha=0.5, figsize=None, ax=None, grid=False,
diagonal='hist', marker='.', density_kwds=None,
hist_kwds=None, range_padding=0.05, **kwds):
"""
Draw a matrix of scatter plots.
Parameters
----------
frame : DataFrame
alpha : float, optional
amount of transparency applied
figsize : (float,float), optional
a tuple (width, height) in inches
ax : Matplotlib axis object, optional
grid : bool, optional
setting this to True will show the grid
diagonal : {'hist', 'kde'}
pick between 'kde' and 'hist' for
either Kernel Density Estimation or Histogram
plot in the diagonal
marker : str, optional
Matplotlib marker type, default '.'
hist_kwds : other plotting keyword arguments
To be passed to hist function
density_kwds : other plotting keyword arguments
To be passed to kernel density estimate plot
range_padding : float, optional
relative extension of axis range in x and y
with respect to (x_max - x_min) or (y_max - y_min),
default 0.05
kwds : other plotting keyword arguments
To be passed to scatter function
因此colors
或c
似乎被传递给scatter
中的matplotlib
函数作为函数调用中的**kwds
之一。
这是分散函数:
matplotlib.pyplot.scatter(x, y, s=20, c=None, marker='o', cmap=None, norm=None, vmin=None, vmax=None, alpha=None, linewidths=None, verts=None, edgecolors=None, hold=None, data=None, **kwargs)
此处参数为c
而不是color
,但在其他部分color
列为c
的替代参数(如您所料)。
我在matplotlib上发布了一个问题。我会通知你。
截至2016年12月11日的新闻
经过一番讨论后,这个bug已被大熊猫接受,并计划在下一个主要版本中修复。见here on github
基本上,当指定c
时,c
将被发送到matplotlib中的scatter
函数。指定color
后,会发送c
和color
,这会让matplotlib感到困惑。
按照建议的时间,使用c
代替color
答案 1 :(得分:-3)
所以我现在正打电话,无法追踪,但这是我可以告诉你的。 kwarg 是将关键字参数传递给函数的时候。
scatterplot = pd.scatter_matrix(dataframe, alpha=0.3,figsize=(10, 10),diagonal='hist',color=colors, marker='o',grid=True)
color = colors 是关键字参数。现在在函数调用的某个地方看起来像 c 成为关键字参数。我不知道你怎么能改变它,但你可以摆脱你的颜色 Kwarg,这可能会解决现在的问题。否则,您需要查看堆栈跟踪中的那些函数,并找出 c 何时成为kwarg