Iva*_*Lee 2 python matplotlib pandas
我试图在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)
Run Code Online (Sandbox Code Playgroud)
这是错误:
()中的ValueError Traceback(最近一次调用最后)1 scatterplot = pd.scatter_matrix(dataframe,alpha = 0.3,----> 2 figsize =(10,10),diagonal ='hist',color = colors,marker = 'O',网格= TRUE)
scatter_matrix中的/Users/leeivan/anaconda/lib/python2.7/site-packages/pandas/tools/plotting.py(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(boundaries_list [j])
/Users/leeivan/anaconda/lib/python2.7/site-packages/matplotlib/ 初始化 pyc文件在内(AX,*指定参数时,**kwargs)1817
warnings.warn(MSG%(label_namer,闪光功能名),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但不是两者兼而有之; 它们不同但功能重叠.
我在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)
Run Code Online (Sandbox Code Playgroud)
显然,该参数color正在生成错误,同时c正在工作.另一方面,它可能是matplotlib中的一个错误.
这是我发现的,看看pandas功能:
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
Run Code Online (Sandbox Code Playgroud)
所以看来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)
Run Code Online (Sandbox Code Playgroud)
这里的参数是c和不是color,但在其他部分color被列为c(如你所料)的替代.
我在matplotlib上发布了一个问题.我会随时通知你.
新闻自2016年12月12日起
经过一番讨论后,这个bug已被大熊猫接受,并计划在下一个主要版本中修复.在github上看到这里
基本上在c指定时,c会发送到scattermatplotlib中的函数.何时color指定,发送c和color发送,使matplotlib混淆.
根据建议,暂时使用c而不是color