Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"ValueError: RGBA values should be within 0-1 range" after upgrading matplotlib

I have upgraded to matplotlib 3.0.2 and the script below I used to use for my 3d plots throws this error now: ValueError: RGBA values should be within 0-1 range. Tested with the 2.0.2 version and it works there... Tried to google for similar issues but couldn't find a workaround so asking this smart community for help...

test = pd.DataFrame({'cluster': ["0", "1", "2"],
    'x': [2, 3, 1],
    'y': [10, 5, -2],
    'z': [-10, -5, 2]})

fig = plt.figure(figsize=(7,7))

ax = Axes3D(fig) 

x=test['x']
y=test['y']
z=test['z']
clusters = test['cluster']

ax.scatter(x, y, z, c=clusters, marker='x', cmap='tab20b', depthshade=False)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')

plt.show()
like image 998
aviss Avatar asked Dec 24 '22 02:12

aviss


1 Answers

Your clusters are strings. Prior to matplotlib 2.1 arrays were converted to numbers coincidentally such that the code would run. From matplotlib 2.1 you need to supply numbers in order to have them interpreted as such. E.g.

clusters = test['cluster'].astype(int)
like image 64
ImportanceOfBeingErnest Avatar answered May 19 '23 12:05

ImportanceOfBeingErnest