由于不推荐使用多维索引的非元组序列,因此Seaborn绘制关联图失败

问题描述 投票:0回答:1

我最近开始运行最新版本的anaconda(2018.12与py37_0),我正在使用jupyter笔记本来运行我的代码。在安装此版本之前,我使用的是Python 3.2.2,下面提供的代码可以顺利运行。该代码使用Seaborn使用从pandas数据帧中获取的变量生成相关图。但是,现在我得到“IndexError:tuple index超出范围”,我不知道如何修复该错误。

之前已报道过类似的问题:

FutureWarning: Using a non-tuple sequence for multidimensional indexing is deprecated use `arr[tuple(seq)]` instead of `arr[seq]`

FutureWarning: Using a non-tuple sequence for multidimensional indexing is deprecated use `arr[tuple(seq)]`

这些解决方案似乎都不适合我。

最后,绘制数据框中变量的相关性的代码来自:

Correlation matrix plot with coefficients on one side, scatterplots on another, and distributions on diagonal

创建pandas数据帧的数据来自Kaggle竞赛的csv文件:

https://archive.ics.uci.edu/ml/machine-learning-databases/wine-quality/

我使用了“winequality-white.csv”文件

import pandas as np
import seaborn as sns
import numpy as np

df = pd.read_csv('winequality-white.csv')

def corrdot(*args, **kwargs):
    corr_r = args[0].corr(args[1], 'pearson')
    corr_text = f"{corr_r:2.2f}".replace("0.", ".")
    ax = plt.gca()
    ax.set_axis_off()
    marker_size = abs(corr_r) * 10000
    ax.scatter(.5, .5, marker_size, corr_r, alpha=0.6, cmap="coolwarm",
               vmin=-1, vmax=1, transform=ax.transAxes)
    font_size = abs(corr_r) * 40 + 5
    ax.annotate(corr_text, [.5, .5,],  xycoords="axes fraction",
                ha='center', va='center', fontsize=font_size)

sns.set(style='white', font_scale=1.6)

g = sns.PairGrid(df, aspect=1.4, diag_sharey=False)
g.map_lower(sns.regplot, lowess=True, ci=False, line_kws={'color': 'black'})
g.map_diag(sns.distplot, kde_kws={'color': 'black'})
g.map_upper(corrdot)

预期结果可以作为答案:

Correlation matrix plot with coefficients on one side, scatterplots on another, and distributions on diagonal

实际结果:

C:\ Users \ Public \ anaconda3 \ lib \ site-packages \ scipy \ stats \ stats.py:1713:FutureWarning:不推荐使用非元组序列进行多维索引;使用arr[tuple(seq)]而不是arr[seq]。将来,这将被解释为数组索引arr[np.array(seq)],这将导致错误或不同的结果。 return np.add.reduce(sorted [indexer] * weights,axis = axis)/ sumval

追溯:

IndexError                                Traceback (most recent call last)
<ipython-input-8-68b4a938aa72> in <module>
     17 g.map_lower(sns.regplot, lowess=True, ci=False, line_kws={'color': 'black'})
     18 g.map_diag(sns.distplot, kde_kws={'color': 'black'})
---> 19 g.map_upper(corrdot)

C:\Users\Public\anaconda3\lib\site-packages\seaborn\axisgrid.py in map_upper(self, func, **kwargs)
   1488                 color = self.palette[k] if kw_color is None else kw_color
   1489                 func(data_k[x_var], data_k[y_var], label=label_k,
-> 1490                      color=color, **kwargs)
   1491 
   1492             self._clean_axis(ax)

<ipython-input-8-68b4a938aa72> in corrdot(*args, **kwargs)
      7     marker_size = abs(corr_r) * 10000
      8     ax.scatter(.5, .5, marker_size, corr_r, alpha=0.6, cmap="coolwarm",
----> 9                vmin=-1, vmax=1, transform=ax.transAxes)
     10     font_size = abs(corr_r) * 40 + 5
     11     ax.annotate(corr_text, [.5, .5,],  xycoords="axes fraction",

C:\Users\Public\anaconda3\lib\site-packages\matplotlib\__init__.py in inner(ax, data, *args, **kwargs)
   1808                         "the Matplotlib list!)" % (label_namer, func.__name__),
   1809                         RuntimeWarning, stacklevel=2)
-> 1810             return func(ax, *args, **kwargs)
   1811 
   1812         inner.__doc__ = _add_data_doc(inner.__doc__,

C:\Users\Public\anaconda3\lib\site-packages\matplotlib\axes\_axes.py in scatter(self, x, y, s, c, marker, cmap, norm, vmin, vmax, alpha, linewidths, verts, edgecolors, **kwargs)
   4209             try:  # First, does 'c' look suitable for value-mapping?
   4210                 c_array = np.asanyarray(c, dtype=float)
-> 4211                 n_elem = c_array.shape[0]
   4212                 if c_array.shape in xy_shape:
   4213                     c = np.ma.ravel(c_array)

IndexError: tuple index out of range

seaborn python-3.7
1个回答
0
投票

分散的参数应该是列表或数组。因此plt.scatter(1,1, c=1, vmin=0, vmax=2)不起作用,但是

plt.scatter([1],[1], c=[1], vmin=0, vmax=2)

将工作。

© www.soinside.com 2019 - 2024. All rights reserved.