问题
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 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
回答1:
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.
来源:https://stackoverflow.com/questions/51658832/i-am-trying-to-plot-clusters-with-unique-color-but-getting-error