【问题标题】:matplotlib & seaborn: ValueError: Supply a 'c' kwarg or a 'color' kwarg but not both; they differ but their functionalities overlapmatplotlib & seaborn: ValueError: 提供一个 'c' kwarg 或一个 'color' kwarg 但不能同时提供;它们不同,但它们的功能重叠
【发布时间】:2017-03-01 16:29:37
【问题描述】:

我正在尝试使用以下代码在一个大图中创建多个 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},所以默认颜色是无。根据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.

我不明白为什么会出现此错误。有人有什么主意吗?谢谢!

【问题讨论】:

    标签: python matplotlib seaborn


    【解决方案1】:

    这很好用,设置绘图颜色并通过颜色图传递到散点图:

    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()
    

    【讨论】:

      猜你喜欢
      • 2017-03-17
      • 1970-01-01
      • 2017-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-30
      • 1970-01-01
      • 2020-02-24
      相关资源
      最近更新 更多