python - Matplotlib 3D 颜色基于 Z 轴值的点

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

最佳答案

如果你看看画廊,你就会找到答案。您需要将一个数组传递给c=colors。请参阅: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()

enter image description here

关于python - Matplotlib 3D 颜色基于 Z 轴值的点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39797672/

相关文章:

python - 'NoneType' 对象在 scrapy\twisted\openssl 中没有属性 '_app_data'

python - Pandas Groupby 有坏行

python - 了解 matplotlib 中 subplot 和 add_subplot(散点)图之间的区别

python - Matplotlib 窗口大小、标题等

python - Google 生成的图表和 matplotlib 图表不同

python - 使用 Pandas/matplotlib 绘制日均值时更改日期时间刻度的格式

python - scrapy 503 服务在 starturl 上不可用

python - 如何在 Panda 的 from_dict (Python) 中使用 dtype 参数

Python、Windows、Ansi——再次编码

Python:为什么特征向量与第一个 PCA 权重不同?