Specify color of each point in scatter plot (matplotlib)

不羁岁月 提交于 2019-12-04 06:04:30

I used a for loop to individually assign each color to each point. Here is my code:

X = [1, 2, 3]
Y = [2, 5, 8]
Z = [6, 4, 5]
colors=["#0000FF", "#00FF00", "#FF0066"]

fig = plt.figure()
ax = fig.add_subplot(111, projection = '3d')


for i in range(len(X)):
    ax.scatter(X[i], Y[i], Z[i], color=colors[i])
plt.show()

The for loop goes point by point (hence the [i] in front of each X,Y,Z value) and gives a color one by one. I used hex colors for my example, but you could probably use something else if you wanted.

If you don't want to use a for loop (which can be very slow for large lists) You can use the scatter command as is with an RGB color list, but you need to specify the colors as a vector of RGB (or RGBA) values between 0 and 1

X = [0, 1, 2]
Y = [0, 1, 2]
Z = [0, 1, 2]
C = np.array([[255, 0, 0], [0, 255, 0], [0, 0, 255]])
fig = plt.figure()
ax = fig.add_subplot(111, projection = '3d')

ax.scatter(X, Y, Z, c = C/255.0)
plt.show()

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!