用Python绘制三维立体图形,从易到难(附完整代码)

本文仅仅梳理最基本的绘图方法。

一、初始化

假设已经安装了matplotlib工具包。

利用matplotlib.figure.Figure创建一个图框:

1

2

3

4

import matplotlib.pyplot as plt

from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure()

ax = fig.add_subplot(111, projection='3d')

二、直线绘制(Line plots)

基本用法:

1

ax.plot(x,y,z,label=' ')

code:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

import matplotlib as mpl

from mpl_toolkits.mplot3d import Axes3D

import numpy as np

import matplotlib.pyplot as plt

mpl.rcParams['legend.fontsize'= 10

fig = plt.figure()

ax = fig.gca(projection='3d')

theta = np.linspace(-4 * np.pi, 4 * np.pi, 100)

= np.linspace(-22100)

= z**2 + 1

= * np.sin(theta)

= * np.cos(theta)

ax.plot(x, y, z, label='parametric curve')

ax.legend()

plt.show()

三、散点绘制(Scatter plots)

基本用法:

1

ax.scatter(xs, ys, zs, s=20, c=None, depthshade=True*args, *kwargs)

  • xs,ys,zs:输入数据;
  • s:scatter点的尺寸
  • c:颜色,如c = 'r'就是红色;
  • depthshase:透明化,True为透明,默认为True,False为不透明
  • *args等为扩展变量,如maker = 'o',则scatter结果为’o‘的形状

code:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

from mpl_toolkits.mplot3d import Axes3D

import matplotlib.pyplot as plt

import numpy as np

def randrange(n, vmin, vmax):

    '''

    Helper function to make an array of random numbers having shape (n, )

    with each number distributed Uniform(vmin, vmax).

    '''

    return (vmax - vmin)*np.random.rand(n) + vmin

fig = plt.figure()

ax = fig.add_subplot(111, projection='3d')

= 100

# For each set of style and range settings, plot n random points in the box

# defined by x in [23, 32], y in [0, 100], z in [zlow, zhigh].

for c, m, zlow, zhigh in [('r''o'-50-25), ('b''^'-30-5)]:

    xs = randrange(n, 2332)

    ys = randrange(n, 0100)

    zs = randrange(n, zlow, zhigh)

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

四、线框图(Wireframe plots)

基本用法:

1

ax.plot_wireframe(X, Y, Z, *args, **kwargs)

  • X,Y,Z:输入数据
  • rstride:行步长
  • cstride:列步长
  • rcount:行数上限
  • ccount:列数上限

code:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

from mpl_toolkits.mplot3d import axes3d

import matplotlib.pyplot as plt

fig = plt.figure()

ax = fig.add_subplot(111, projection='3d')

# Grab some test data.

X, Y, Z = axes3d.get_test_data(0.05)

# Plot a basic wireframe.

ax.plot_wireframe(X, Y, Z, rstride=10, cstride=10)

plt.show()

五、表面图(Surface plots)

基本用法:

1

ax.plot_surface(X, Y, Z, *args, **kwargs)

  • X,Y,Z:数据
  • rstride、cstride、rcount、ccount:同Wireframe plots定义
  • color:表面颜色
  • cmap:图层

code:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

from mpl_toolkits.mplot3d import Axes3D

import matplotlib.pyplot as plt

from matplotlib import cm

from matplotlib.ticker import LinearLocator, FormatStrFormatter

import numpy as np

fig = plt.figure()

ax = fig.gca(projection='3d')

# Make data.

= np.arange(-550.25)

= np.arange(-550.25)

X, Y = np.meshgrid(X, Y)

= np.sqrt(X**2 + Y**2)

= np.sin(R)

# Plot the surface.

surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm,

                       linewidth=0, antialiased=False)

# Customize the z axis.

ax.set_zlim(-1.011.01)

ax.zaxis.set_major_locator(LinearLocator(10))

ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))

# Add a color bar which maps values to colors.

fig.colorbar(surf, shrink=0.5, aspect=5)

plt.show()

这里,我为您精心准备了一份全面的Python学习大礼包,完全免费分享给每一位渴望成长、希望突破自我现状却略感迷茫的朋友。无论您是编程新手还是希望深化技能的开发者,都欢迎加入我们的学习之旅,共同交流进步!

🌟 学习大礼包包含内容:

  1. Python全领域学习路线图一目了然,指引您从基础到进阶,再到专业领域的每一步学习路径,明确各方向的核心知识点。

  2. 超百节Python精品视频课程:涵盖Python编程的必备基础知识、高效爬虫技术、以及深入的数据分析技能,让您技能全面升级。

  3. 实战案例集锦:精选超过100个实战项目案例,从理论到实践,让您在解决实际问题的过程中,深化理解,提升编程能力。

  4. 华为独家Python漫画教程:创新学习方式,以轻松幽默的漫画形式,让您随时随地,利用碎片时间也能高效学习Python。

  5. 互联网企业Python面试真题集:精选历年知名互联网企业面试真题,助您提前备战,面试准备更充分,职场晋升更顺利。

👉 立即领取方式:只需【点击这里】,即刻解锁您的Python学习新篇章!让我们携手并进,在编程的海洋里探索无限可能!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值