【python系列】使用mayavi画3d散点图

如何使用mayavi,请参见上一篇文章。


1.使用mayavi

代码

import enthought.mayavi.mlab as mylab
import numpy as np
x, y, z, value = np.random.random((4, 40))
mylab.points3d(x, y, z, value)
mylab.show()

效果

【python系列】使用mayavi画3d散点图_第1张图片




2.使用mpl_toolkits

代码

import pylab as p
import mpl_toolkits.mplot3d.axes3d as p3
import numpy as np
#data is an ndarray with the necessary data and colors is an ndarray with #'b', 'g' and 'r' to paint each point according to its class ...
fig=p.figure()
point_num=100
data=np.random.random((point_num,3))
colors=[['b','g','r'][int(i*2.999)] for i in np.random.random((point_num,1))]
ax = p3.Axes3D(fig)
ax.scatter(data[:,0], data[:,1], data[:,2], c=colors)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
fig.add_axes(ax)
p.show()

效果

【python系列】使用mayavi画3d散点图_第2张图片




3.带标签的

代码

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()
ax = fig.add_subplot(111, projection='3d')
n = 100

for c, m, zl, zh in [('r', 'o', -50, -25), ('b', '^', -30, -5)]:
    xs = randrange(n, 23, 32)
    ys = randrange(n, 0, 100)
    zs = randrange(n, zl, zh)
    ax.scatter(xs, ys, zs, c=c, marker=m)

ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
plt.show()

效果

【python系列】使用mayavi画3d散点图_第3张图片

你可能感兴趣的:(python,绘图)