如何将由pyplot.Spectral创建的调色板用于循环中的散点图?

时间:2019-07-08 13:43:55

标签: python matplotlib k-means spectral

我有一个二维的点数据集,我想使用K-means技术进行分类。

数据:

import numpy as np

x1 = np.array([3,1,1,2,1,6,6,6,5,6,7,8,9,8,9,9,8])
x2 = np.array([5,4,5,6,5,8,6,7,6,7,1,2,1,2,3,2,3])
X = np.array(list(zip(x1,x2))).reshape(len(x1), 2)

我想对1到9之间的簇数进行求和,以测试散点图上的最终分布。因此,我计算了数据集的质心。

from scipy.spatial.distance import cdist
import matplotlib.pyplot as plt

max_k = 10
K = range(1,max_k)
centroid = [sum(X)/len(X) for k in K]
sst = sum(np.min(cdist(X, centroid, "euclidean"), axis = 1))

然后使用rgb为每次迭代创建一个具有cm.Spectral颜色的调色板。

color_palette = [plt.cm.Spectral(float(k)/max_k) for k in K]

并在我遍历k的循环中使用它:

from sklearn.cluster import KMeans
import pandas as pd

ssw = []
for k in K:
    kmeanModel = KMeans(n_clusters=k).fit(X)

    centers = pd.DataFrame(kmeanModel.cluster_centers_)
    labels = kmeanModel.labels_

    ssw_k = sum(np.min(cdist(X, kmeanModel.cluster_centers_), axis = 1))
    ssw.append(ssw_k)

    label_color = [color_palette[i] for i in labels]

    plt.plot()
    plt.xlim([0,10])
    plt.ylim([0,10])
    plt.title("Clustering for k = %s"%str(k))
    plt.scatter(x1,x2, c=label_color)
    plt.scatter(centers[0], centers[1], c=color_palette, marker = "x")
    plt.show()

我正在用我的Python 3.7.3版本重现此代码,并且从这段代码的源头知道,它在较旧的版本中可以正常工作。将Spectral中的函数matplotlib.pyplot.cm写为小写(spectral)时。

结果是下一个。

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
~/anaconda3/lib/python3.7/site-packages/matplotlib/axes/_axes.py in scatter(self, x, y, s, c, marker, cmap, norm, vmin, vmax, alpha, linewidths, verts, edgecolors, **kwargs)
   4237                     valid_shape = False
-> 4238                     raise ValueError
   4239             except ValueError:

ValueError: 

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
<ipython-input-26-2f513f9c616c> in <module>
     24     plt.title("Clustering for k = %s"%str(k))
     25     plt.scatter(x1,x2, c=label_color)
---> 26     plt.scatter(centers[0], centers[1], c=[i for i in color_palette], marker = "x")
     27     plt.show()

~/anaconda3/lib/python3.7/site-packages/matplotlib/pyplot.py in scatter(x, y, s, c, marker, cmap, norm, vmin, vmax, alpha, linewidths, verts, edgecolors, data, **kwargs)
   2860         vmin=vmin, vmax=vmax, alpha=alpha, linewidths=linewidths,
   2861         verts=verts, edgecolors=edgecolors, **({"data": data} if data
-> 2862         is not None else {}), **kwargs)
   2863     sci(__ret)
   2864     return __ret

~/anaconda3/lib/python3.7/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__,

~/anaconda3/lib/python3.7/site-packages/matplotlib/axes/_axes.py in scatter(self, x, y, s, c, marker, cmap, norm, vmin, vmax, alpha, linewidths, verts, edgecolors, **kwargs)
   4243                         "acceptable for use with 'x' with size {xs}, "
   4244                         "'y' with size {ys}."
-> 4245                         .format(nc=n_elem, xs=x.size, ys=y.size)
   4246                     )
   4247                 # Both the mapping *and* the RGBA conversion failed: pretty

ValueError: 'c' argument has 9 elements, which is not acceptable for use with 'x' with size 1, 'y' with size 1.

我希望每个小组的中心都像小组本身一样上色。

谢谢。

1 个答案:

答案 0 :(得分:0)

尝试通过索引使用相应尺寸的调色板,该索引对应于x和y值的长度,如下所示。

P.S:您的代码在matplotlib 2.2.2上正常工作

for i, k in enumerate(K):
    # rest of your code

    plt.scatter(centers[0], centers[1], c=color_palette[0:i+1], marker = "x")
    print (centers[0].values)
    plt.show()