经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » Python » 查看文章
python?绘制3D图案例分享
来源:jb51  时间:2022/8/1 9:57:34  对本文有异议

1.散点图

代码

  1. # This import registers the 3D projection, but is otherwise unused.
  2. from mpl_toolkits.mplot3d import Axes3D # noqa: F401 unused import
  3.  
  4. import matplotlib.pyplot as plt
  5. import numpy as np
  6.  
  7. # Fixing random state for reproducibility
  8. np.random.seed(19680801)
  9. def randrange(n, vmin, vmax):
  10. '''
  11. Helper function to make an array of random numbers having shape (n, )
  12. with each number distributed Uniform(vmin, vmax).
  13. '''
  14. return (vmax - vmin)*np.random.rand(n) + vmin
  15.  
  16. fig = plt.figure()
  17. ax = fig.add_subplot(111, projection='3d')
  18. n = 100
  19. # For each set of style and range settings, plot n random points in the box
  20. # defined by x in [23, 32], y in [0, 100], z in [zlow, zhigh].
  21. for m, zlow, zhigh in [('o', -50, -25), ('^', -30, -5)]:
  22. xs = randrange(n, 23, 32)
  23. ys = randrange(n, 0, 100)
  24. zs = randrange(n, zlow, zhigh)
  25. ax.scatter(xs, ys, zs, marker=m)
  26. ax.set_xlabel('X Label')
  27. ax.set_ylabel('Y Label')
  28. ax.set_zlabel('Z Label')
  29. plt.show()

输出:

输入的数据格式

这个输入的三个维度要求是三列长度一致的数据,可以理解为3个length相等的list。
用上面的scatter或者下面这段直接plot也可以。

  1. fig = plt.figure()
  2. ax = fig.gca(projection='3d')
  3. ax.plot(h, z, t, '.', alpha=0.5)
  4. plt.show()

输出:

2.三维表面 surface

代码

  1. x = [12.7, 12.8, 12.9]
  2. y = [1, 2, 3, 4]
  3. temp = pd.DataFrame([[7,7,9,9],[2,3,4,5],[1,6,8,7]]).T
  4. X,Y = np.meshgrid(x,y) # 形成网格化的数据
  5. temp = np.array(temp)
  6. fig = plt.figure(figsize=(16, 16))
  7. ax = fig.gca(projection='3d')
  8. ax.plot_surface(Y,X,temp,rcount=1, cmap=cm.plasma, linewidth=1, antialiased=False,alpha=0.5) #cm.plasma
  9. ax.set_xlabel('zone', color='b', fontsize=20)
  10. ax.set_ylabel('h2o', color='g', fontsize=20)
  11. ax.set_zlabel('Temperature', color='r', fontsize=20)

output:

输入的数据格式

这里x和y原本都是一维list,通过np.meshgrid可以将其形成4X3的二维数据,如下图所示:

而第三维,得是4X3的2维的数据,才能进行画图

scatter + surface图形展示

3. 三维瀑布图waterfall

代码

  1. from matplotlib.collections import PolyCollection
  2. import matplotlib.pyplot as plt
  3. from matplotlib import colors as mcolors
  4. import numpy as np
  5.  
  6. axes=plt.axes(projection="3d")
  7.  
  8. def colors(arg):
  9. return mcolors.to_rgba(arg, alpha=0.6)
  10. verts = []
  11. z1 = [1, 2, 3, 4]
  12. x1 = np.arange(0, 10, 0.4)
  13. for z in z1:
  14. y1 = np.random.rand(len(x1))
  15. y1[0], y1[-1] = 0, 0
  16. verts.append(list(zip(x1, y1)))
  17. # print(verts)
  18. poly = PolyCollection(verts, facecolors=[colors('r'), colors('g'), colors('b'),
  19. colors('y')])
  20. poly.set_alpha(0.7)
  21. axes.add_collection3d(poly, zs=z1, zdir='y')
  22. axes.set_xlabel('X')
  23. axes.set_xlim3d(0, 10)
  24. axes.set_ylabel('Y')
  25. axes.set_ylim3d(-1, 4)
  26. axes.set_zlabel('Z')
  27. axes.set_zlim3d(0, 1)
  28. axes.set_title("3D Waterfall plot")
  29. plt.show()

输出:

输入的数据格式

这个的输入我还没有完全搞懂,导致我自己暂时不能复现到其他数据,等以后懂了再回来补充。

4. 3d wireframe

code

  1. from mpl_toolkits.mplot3d import axes3d
  2. import matplotlib.pyplot as plt
  3.  
  4. fig, (ax1, ax2) = plt.subplots(
  5. 2, 1, figsize=(8, 12), subplot_kw={'projection': '3d'})
  6.  
  7. # Get the test data
  8. X, Y, Z = axes3d.get_test_data(0.05)
  9.  
  10. # Give the first plot only wireframes of the type y = c
  11. ax1.plot_wireframe(X, Y, Z, rstride=10, cstride=0)
  12. ax1.set_title("Column (x) stride set to 0")
  13.  
  14. # Give the second plot only wireframes of the type x = c
  15. ax2.plot_wireframe(X, Y, Z, rstride=0, cstride=10)
  16. ax2.set_title("Row (y) stride set to 0")
  17. plt.tight_layout()
  18. plt.show()

output:

输入的数据格式

与plot_surface的输入格式一样,X,Y原本为一维list,通过np.meshgrid形成网格化数据。Z为二维数据。其中注意调节rstride、cstride这两个值实现行列间隔的调整。

自己试了下:

到此这篇关于python 绘制3D图案例分享的文章就介绍到这了,更多相关python 绘制3D图内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号