Reputation: 69
I am currently scattering points in a 3D graph. My X,Y and Z are lists (len(Z)=R). However, I would like to give them a color based on their Z value. For example if Z>1 the color would be red, Z>2 blue Z>3 pink and so on. My current code is:
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(X,Y,Z,for k in range (R): if Z>1: color=['red'])
plt.show()
Upvotes: 2
Views: 5482
Reputation: 3871
If you see the gallery, you will find the answer. You need to pass an array to c=colors
. See these: 1, 2.
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
def randrange(n, vmin, vmax):
return (vmax - vmin)*np.random.rand(n) + vmin
fig = plt.figure(figsize=(8,5))
ax = fig.add_subplot(111, projection='3d')
n = 100
xs = randrange(n, 23, 32)
ys = randrange(n, 0, 100)
zs = randrange(n, 0, 50)
scat = ax.scatter(xs, ys, zs, c=zs, marker="o", cmap="viridis")
plt.colorbar(scat)
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
plt.show()
Upvotes: 4