【问题标题】:I am trying to plot clusters with unique color but getting error我正在尝试绘制具有独特颜色的集群但出现错误
【发布时间】:2019-01-10 12:47:31
【问题描述】:

我只是想绘制 x、y,然后用簇给它们上色。每个集群都应该有唯一的颜色。但我收到错误。我尝试不使用 numpy 数组,但仍然出现错误。

" 形状为 (5113,) 的 c 不能作为 x 大小为 5113、y 大小为 5113"" 的颜色序列

x = np.array(df['ip_indexed'])
print(x.shape)
y = np.array(df['geohash_indexed'])
print(y.shape)
labels = np.array(df['clusters'])
print(labels.shape)

''' output. 
(5113,)
(5113,)
(5113,)
 '''

LABEL_COLOR_MAP = {9066 : 'r',
               9068: 'silver',
               17182 : 'k',
               17183: 'c',
               17184: 'indigo',
               17185:  'tan',
               17186: 'plum',
               17187: 'yello',
               17188:'olive',
               17189:'deeppink'
               }

label_color = np.array([LABEL_COLOR_MAP[l] for l in labels])
label_color.shape

plt.figure(figsize=(50, 10))
plt.xlabel("ip_address", fontsize= 20)
plt.ylabel("geohash", fontsize= 20)
plt.title("clusters", fontsize= 50)
colors = np.random.rand(5113)
plt.scatter(x, y , c = label_color)

ValueError: c of shape (5113,) not acceptable as a color sequence for x with size 5113, y with size 5113

【问题讨论】:

  • 你能分享一个数据点少的例子吗,比如说xy,大小为3
  • "yello" 不是有效颜色。 Appart没有理由会失败。你能提供一个minimal reproducible example,即一个可以重现的可运行代码吗?
  • x= [414 414 414 ... 420 421 422] y=[ 0 0 2 ... 210 211 212]
  • 标签 = [ 9066 9066 9068 ... 17187 17188 17189]
  • yello 不是问题。我纠正了它,仍然是同样的问题。

标签: python matplotlib scatter-plot


【解决方案1】:

我不久前遇到了同样的问题,并在寻找解决方案时偶然发现了这篇文章。我可以肯定地说的事情: 在plt.scatter(x, y , c = label_color) 行中,xyc 的形状必须相同,而且我相信,如果它们是 pandas 数据框,这三个都是最好的。我所有成功对象的形状都是(5113, 1),而不是你的(5113,)。我知道您正在测试 print() 语句中的形状,但似乎有些不对劲。

我绝不是专业人士,在某些方面我可能错了,但这就是我找到自己解决方案的方式。希望它可以将您和其他人推向正确的方向。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-13
    • 2019-08-27
    • 2023-02-14
    • 2020-12-21
    • 2019-03-21
    • 1970-01-01
    • 2021-08-01
    • 2016-07-26
    相关资源
    最近更新 更多