gpt4 book ai didi

python - matplotlib 和seaborn : ValueError: Supply a 'c' kwarg or a 'color' kwarg but not both; they differ but their functionalities overlap

转载 作者:太空宇宙 更新时间:2023-11-03 16:02:57 24 4
gpt4 key购买 nike

我正在尝试使用以下代码在一个大图中创建多个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} ,默认颜色为无。根据seaborn文档https://seaborn.github.io/generated/seaborn.regplot.html#seaborn.regplot

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

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

最佳答案

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

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

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

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