簡體   English   中英

python matplotlib:分配cmap進行多個顏色散射圖

[英]python matplotlib : assign cmap to make multiple color scattering plot

我正在嘗試使用matplotlib和seaborn來創建散點圖。 如果整個圖僅是一種顏色,則效果很好,如下所示:

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的值,例如:

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點,例如:

[1,0,0]
[0,1,0]
[1,0,0]
[0,1,0]
   :
   :

然后我得到了錯誤:

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時做錯了什么? 謝謝!

我自己一年前使用的代碼就遇到了這個問題。 (我可能已經從Python 2切換到Python 3,這也許可以解釋該錯誤。)

重申

我仔細研究了一下代碼,正如您所指出的,錯誤發生在

--> 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 )都需要具備以下兩個功能:

  • 不必具有shape屬性
  • 但是,如果我確實具有shape屬性,則該屬性必須至少具有2個維度。

根本問題

好吧,這是有問題的:一個熊貓 Series或一個numpy ndarray (或者我猜是其他幾種數據結構)具有一個只能具有1維的shape屬性。

例如,當我遇到問題時,我遇到了如下問題:

col.shape

(2506,)

這意味着我的col變量(在我的情況下為pandas Series對象) 具有一個shape 並且該形狀只有一個尺寸。

對我來說,如何解決這個問題並不明顯。 我試圖將我的“ 熊貓 Series加到一個list ,但這並不能解決問題。 我試圖只傳遞2D pandas DataFrame ,每個列都相同,但這並不能解決。

潛在修復(針對無法阻止的人)

在查看源代碼時 ,我不知道如何解決問題。 正確的解決方案似乎是在第355行中添加另一個如下所示的檢查:

355             if not hasattr(kws['color'], 'shape') or len(kws['color'].shape) < 2 or kws['color'].shape[1] < 4:

但是我沒有精力(或時間)來解決派生源和提交修復程序的麻煩。 :(

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM