matplotlib& seaborn:ValueError:提供'c'kwarg或'color'kwarg但不是两者兼而有之;它们不同但功能重叠

时间:2016-10-19 20:59:42

标签: python matplotlib seaborn

我正在尝试使用以下代码在一个大图中创建多个seaborn regplot:

%matplotlib notebook
import seaborn as sns
from itertools import combinations
import matplotlib.pyplot as plt

pairs = list(combinations(pandas_transformed.drop(['prediction'],axis=1).columns, 2))
col = pandas_transformed.prediction.map({0: [1,0,0], 1:[0,1,0]})

fig, axes = plt.subplots(len(pairs) // 3, 3, figsize=(12, 108))
for i, pair in enumerate(pairs):
    d = pandas_transformed[list(pair)]
    ax = axes[i // 3, i % 3]
    #d.plot.scatter(*pair, ax=ax, c=col, linewidths=0, s=2, alpha = 0.7)
    sns.regplot(x = pair[0], y = pair[1], data = d, fit_reg = False, ax = ax, x_jitter = True,\
             scatter_kws={"c": col}, line_kws = {})

fig.tight_layout()

但是,我收到以下错误:

ValueErrorTraceback (most recent call last)
<ipython-input-12-ae2676825628> in <module>()
     12     ax = axes[i // 3, i % 3]
     13     #d.plot.scatter(*pair, ax=ax, c=col, linewidths=0, s=2, alpha = 0.7)
---> 14     sns.regplot(x = pair[0], y = pair[1], data = d, fit_reg = False, ax = ax, x_jitter = True,             scatter_kws={"c": col}, line_kws = {})
     15 
     16 fig.tight_layout()

/usr/local/lib/python2.7/dist-packages/seaborn/linearmodels.pyc in regplot(x, y, data, x_estimator, x_bins, x_ci, scatter, fit_reg, ci, n_boot, units, order, logistic, lowess, robust, logx, x_partial, y_partial, truncate, dropna, x_jitter, y_jitter, label, color, marker, scatter_kws, line_kws, ax)
    777     scatter_kws["marker"] = marker
    778     line_kws = {} if line_kws is None else copy.copy(line_kws)
--> 779     plotter.plot(ax, scatter_kws, line_kws)
    780     return ax
    781 

/usr/local/lib/python2.7/dist-packages/seaborn/linearmodels.pyc in plot(self, ax, scatter_kws, line_kws)
    328         # Draw the constituent plots
    329         if self.scatter:
--> 330             self.scatterplot(ax, scatter_kws)
    331         if self.fit_reg:
    332             self.lineplot(ax, line_kws)

/usr/local/lib/python2.7/dist-packages/seaborn/linearmodels.pyc in scatterplot(self, ax, kws)
    357 
    358             x, y = self.scatter_data
--> 359             ax.scatter(x, y, **kws)
    360         else:
    361             # TODO abstraction

/usr/local/lib/python2.7/dist-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:

/usr/local/lib/python2.7/dist-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.

错误实在令人困惑。由于我只提供scatter_kws={"c": col},因此默认颜色为None。并根据https://seaborn.github.io/generated/seaborn.regplot.html#seaborn.regplot

的seaborn文件
color : matplotlib color
Color to apply to all plot elements; will be superseded by colors passed in scatter_kws or line_kws.

我不明白为什么我会收到此错误。有谁有想法吗?谢谢!

1 个答案:

答案 0 :(得分:1)

This works fine, setting a plot color and passing through a colormap to the scatter:

import seaborn as sns
tips = sns.load_dataset("tips")
sns.regplot(x='total_bill', y='tip',data=tips, 
            scatter_kws={'c':sns.color_palette()}, 
            color='red')
plt.show()

enter image description here