分享

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

 啊司com 2017-08-11

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


1.使用mayavi

代码

[python] view plain copy
  1. import enthought.mayavi.mlab as mylab  
  2. import numpy as np  
  3. x, y, z, value = np.random.random((4, 40))  
  4. mylab.points3d(x, y, z, value)  
  5. mylab.show()  

效果





2.使用mpl_toolkits

代码

[python] view plain copy
  1. import pylab as p  
  2. import mpl_toolkits.mplot3d.axes3d as p3  
  3. import numpy as np  
  4. #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 ...  
  5. fig=p.figure()  
  6. point_num=100  
  7. data=np.random.random((point_num,3))  
  8. colors=[['b','g','r'][int(i*2.999)] for i in np.random.random((point_num,1))]  
  9. ax = p3.Axes3D(fig)  
  10. ax.scatter(data[:,0], data[:,1], data[:,2], c=colors)  
  11. ax.set_xlabel('X')  
  12. ax.set_ylabel('Y')  
  13. ax.set_zlabel('Z')  
  14. fig.add_axes(ax)  
  15. p.show()  

效果






3.带标签的

代码

[python] view plain copy
  1. import numpy as np  
  2. from mpl_toolkits.mplot3d import Axes3D  
  3. import matplotlib.pyplot as plt  
  4.   
  5. def randrange(n, vmin, vmax):  
  6.     return (vmax-vmin)*np.random.rand(n) + vmin  
  7.   
  8. fig = plt.figure()  
  9. ax = fig.add_subplot(111, projection='3d')  
  10. n = 100  
  11.   
  12. for c, m, zl, zh in [('r', 'o', -50, -25), ('b', '^', -30, -5)]:  
  13.     xs = randrange(n, 23, 32)  
  14.     ys = randrange(n, 0, 100)  
  15.     zs = randrange(n, zl, zh)  
  16.     ax.scatter(xs, ys, zs, c=c, marker=m)  
  17.   
  18. ax.set_xlabel('X Label')  
  19. ax.set_ylabel('Y Label')  
  20. ax.set_zlabel('Z Label')  
  21. plt.show()  

效果



    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约