问题描述
我只是想绘制x,y,然后用聚类为它们着色.每个群集应具有唯一的颜色.但是我出错了.我尝试不使用 numpy 数组,但仍然出错.
I am just trying to plot x, y and then color them with clusters. Each cluster should have unique color. But i am getting error. I tried without using numpy array but still getting error.
" c 的形状 (5113,) 不能作为 x 大小为 5113、y 大小为 5113 的颜色序列""
" c of shape (5113,) not acceptable as a color sequence for x with size 5113, y with size 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
推荐答案
我刚才遇到了同样的问题,在寻找解决方案时偶然发现了这篇文章.我可以肯定地说的话:在 plt.scatter(x, y , c = label_color)
行中,x
、y
和 c必须相同,我相信,如果这三个都是熊猫数据框,那么这三个都是最好的.我所有成功对象的形状都是
(5113, 1)
而不是你的 (5113,)
.我知道您正在 print()
语句中测试形状,但似乎有些不对劲.
I was facing the same issue moments ago and stumbled across this post in searching for a solution. Things that I can definitively say:In the line
plt.scatter(x, y , c = label_color)
the shape of x
, y
, and c
needs to be the same and, I believe, all three are best if they are a pandas dataframe. The shape of all of my successful objects was (5113, 1)
rather than your (5113,)
. I recognize that you are testing the shape in your print()
statements, but something seems amiss.
我绝不是专业人士,在某些方面我可能是错的,但这就是我提出自己的解决方案的方式.希望它可以在正确的方向上推动您和其他人.
I am by no means a professional and I may be wrong in some of this, but this was how I came to my own solution. Hopefully it might nudge you and others in the right direction.
这篇关于我正在尝试绘制具有独特颜色的聚类,但出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!