首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >python matplotlib :赋值cmap绘制多色散射图

python matplotlib :赋值cmap绘制多色散射图
EN

Stack Overflow用户
提问于 2016-10-20 06:38:14
回答 1查看 565关注 0票数 0

我正在尝试使用matplotlib和seaborn来创建散点图。如果整个绘图只有一种颜色,则效果很好,如下所示:

代码语言:javascript
复制
sns.regplot(x = pair[0], y = pair[1], data = d, fit_reg = False, ax = ax, x_jitter = True, scatter_kws = {'linewidths':0, 's':2, 'color':'r'})

但是,如果需要,每个数据点的颜色取决于col中的值,如下所示:

代码语言:javascript
复制
col = pandas_df.prediction.map({0: [1,0,0], 1:[0,1,0]})
sns.regplot(x = pair[0], y = pair[1], data = d, fit_reg = False, ax = ax, x_jitter = True, scatter_kws = {'linewidths':0, 's':2, 'cmap':"RGB", 'color':col})

其中pandas_df是一个熊猫数据帧,所以col是一系列RGB点,如下所示:

代码语言:javascript
复制
[1,0,0]
[0,1,0]
[1,0,0]
[0,1,0]
   :
   :

然后我得到了错误:

代码语言:javascript
复制
IndexErrorTraceback (most recent call last)
<ipython-input-12-e17a2dbdd639> in <module>()
     15     #print dtype(col)
     16     d.plot.scatter(*pair, ax=ax, c=col, linewidths=0, s=2, alpha = 0.7)
---> 17     sns.regplot(x = pair[0], y = pair[1], data = d, fit_reg = False, ax = ax, x_jitter = True,                 scatter_kws = {'linewidths':0, 's':2, 'cmap':"RGB", 'color':col})
     18 
     19 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)
    353             kws.setdefault("linewidths", lw)
    354 
--> 355             if not hasattr(kws['color'], 'shape') or kws['color'].shape[1] < 4:
    356                 kws.setdefault("alpha", .8)
    357 

IndexError: tuple index out of range

在这种情况下,我在指定颜色和cmap时做错了什么?谢谢!

EN

回答 1

Stack Overflow用户

发布于 2017-01-14 15:12:49

我自己也遇到了这个问题,代码在一年前就可以工作了。(我可能已经从Python2切换到Python3,这可能解释了这个错误。)

重复

我深入研究了一下代码,正如您所提到的,错误在

代码语言:javascript
复制
--> 355             if not hasattr(kws['color'], 'shape') or kws['color'].shape[1] < 4:
356                 kws.setdefault("alpha", .8)
357 

IndexError: tuple index out of range

如果你看看这里发生了什么,你传递给'color'关键字的任何东西(在你的例子中特别是'color':col )都需要以下两个特性的:

  • It to shape attribute
  • But if I shape属性,该属性必须至少有2个维度。

根源问题

好吧,这里有一个问题: pandas Series或numpy ndarray (或者其他几个数据结构,我猜)都有一个只能有1维的shape属性。

例如,当我遇到这个问题时,我得到了如下内容:

代码语言:javascript
复制
col.shape

(2506,)

这意味着我的col变量(在我的例子中是一个pandas Series对象),有一个shape 形状只有一个维度。

对我来说,如何解决这个问题并不明显。我试图强迫我的熊猫Series进入list,但这并不能解决问题。我试图传递一个2D pandas DataFrame,其中每一列都是相同的,但这并没有解决它。

潜在修复(针对那些不能被吓倒的人)

在浏览source code的过程中,我并不清楚如何解决问题。看起来,正确的解决方法可能是在第355行添加另一个检查,如下所示:

代码语言:javascript
复制
355             if not hasattr(kws['color'], 'shape') or len(kws['color'].shape) < 2 or kws['color'].shape[1] < 4:

但我没有精力(或时间)去经历分叉源代码和提交修复程序的麻烦。:(

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40142642

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档