gpt4 book ai didi

python - 值错误 : Supply a 'c' kwarg or a 'color' kwarg but not both; they differ but their functionalities overlap

转载 作者:太空宇宙 更新时间:2023-11-04 00:44:25 28 4
gpt4 key购买 nike

我尝试运行 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 (most recent call last) 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 in 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(boundaries_list[j])

/Users/leeivan/anaconda/lib/python2.7/site-packages/matplotlib/init.pyc in inner(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 if pre_doc is None:

/Users/leeivan/anaconda/lib/python2.7/site-packages/matplotlib/axes/_axes.pyc in scatter(self, x, y, s, c, marker, cmap, norm, vmin, vmax, alpha, linewidths, verts, edgecolors, **kwargs) 3787
facecolors = co 3788 if c is not None: -> 3789 raise ValueError("Supply a 'c' kwarg or a 'color' kwarg" 3790 " but not both; they differ but" 3791 " their functionalities overlap.")

ValueError: Supply a 'c' kwarg or a 'color' kwarg but not both; they differ but their functionalities overlap.

最佳答案

我在 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 中的错误。

这是我在查看 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

看来 colorsc传递给 scattermatplotlib 中发挥作用作为 **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 年 11 月 12 日的新闻

经过一些讨论,该错误已被 pandas 接受并计划在下一个主要版本中修复。参见 here on github

基本上当 c已指定,c发送到scatter matplotlib 中的函数。当color指定,两者都是ccolor被发送,混淆了 matplotlib。

暂时按照建议使用c而不是 color

关于python - 值错误 : Supply a 'c' kwarg or a 'color' kwarg but not both; they differ but their functionalities overlap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40401491/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com