Matplotlib3D基于颜色的Z轴值

时间:2016-09-30 18:17:13

标签: python matplotlib

我目前在3D图表中散布点。我的X,Y和Z是列表(len(Z)= R)。但是,我想根据它们的Z值给它们一种颜色。例如,如果Z> 1,颜色将是红色,Z> 2蓝色Z> 3粉红色,依此类推。我目前的代码是:

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()

1 个答案:

答案 0 :(得分:1)

如果你看到画廊,你会找到答案。您需要将数组传递给c=colors。请参阅:12

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()

enter image description here