图形预览:
0、import
1
2
3
|
import numpy as np from matplotlib import pyplot as plt from mpl_toolkits.mplot3d import axes3d |
1、开口向上的抛物面
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
|
fig = plt.figure(figsize = ( 9 , 6 ), facecolor = 'khaki' ) ax = fig.gca(projection = '3d' ) # 二元函数定义域平面集 x = np.linspace(start = - 3 , stop = 3 , num = 100 ) y = np.linspace(start = - 3 , stop = 3 , num = 100 ) x, y = np.meshgrid(x, y) # 网格数据 z = np.power(x, 2 ) + np.power(y, 2 ) # 二元函数 z = x**2 + y**2 # 绘图 surf = ax.plot_surface(x = x, y = y, z = z, rstride = 2 , # row stride, 行跨度 cstride = 2 , # column stride, 列跨度 color = 'r' , linewidth = 0.5 , ) # 调整视角 ax.view_init(elev = 7 , # 仰角 azim = 30 # 方位角 ) # 显示图形 plt.show() |
图形:
2、开口向下的抛物面
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
|
fig = plt.figure(figsize = ( 9 , 6 ), facecolor = 'khaki' ) ax = fig.gca(projection = '3d' ) # 二元函数定义域平面集 x = np.linspace(start = - 3 , stop = 3 , num = 100 ) y = np.linspace(start = - 3 , stop = 3 , num = 100 ) x, y = np.meshgrid(x, y) # 网格数据 z = np.power(x, 2 ) + np.power(y, 2 ) # 二元函数 z = x**2 + y**2 # 绘图 surf = ax.plot_surface(x = x, y = y, z = - z, rstride = 2 , # row stride, 行跨度 cstride = 2 , # column stride, 列跨度 color = 'g' , linewidth = 0.5 , ) # 调整视角 ax.view_init(elev = 7 , # 仰角 azim = 30 # 方位角 ) # 显示图形 plt.show() |
图形:
3、用多子区显示不同抛物面
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
fig = plt.figure(figsize = ( 24 , 16 ), facecolor = 'khaki' ) # 二元函数定义域平面集 x = np.linspace(start = - 3 , stop = 3 , num = 100 ) y = np.linspace(start = - 3 , stop = 3 , num = 100 ) x, y = np.meshgrid(x, y) # 网格数据 z = np.power(x, 2 ) + np.power(y, 2 ) # 二元函数 z = x**2 + y**2 # -------------------------------- subplot(221) -------------------------------- ax = fig.add_subplot( 221 , projection = '3d' ) # 开口向上的抛物面 surf = ax.plot_surface(x = x, y = y, z = z, rstride = 2 , # row stride, 行跨度 cstride = 2 , # column stride, 列跨度 color = 'r' , linewidth = 0.5 , ) # -------------------------------- subplot(223) -------------------------------- ax = fig.add_subplot( 223 , projection = '3d' ) # 开口向下的抛物面 surf = ax.plot_surface(x = x, y = y, z = - z, rstride = 2 , # row stride, 行跨度 cstride = 2 , # column stride, 列跨度 color = 'g' , linewidth = 0.5 , ) # -------------------------------- subplot(22, (2,4)) -------------------------------- ax = plt.subplot2grid(shape = ( 2 , 2 ), loc = ( 0 , 1 ), rowspan = 2 , projection = '3d' ) # 开口向上的抛物面 surf1 = ax.plot_surface(x = x, y = y, z = z, rstride = 2 , # row stride, 行跨度 cstride = 2 , # column stride, 列跨度 color = 'r' , linewidth = 0.5 , ) # 开口向下的抛物面 surf2 = ax.plot_surface(x = x, y = y, z = - z, rstride = 2 , # row stride, 行跨度 cstride = 2 , # column stride, 列跨度 color = 'g' , linewidth = 0.5 , ) # 调整视角 ax.view_init(elev = 7 , # 仰角 azim = 30 # 方位角 ) # -------------------------------- fig -------------------------------- # 调整子区布局 fig.subplots_adjust(wspace = 0.1 , # width space hspace = 0.15 # height space ) # 显示图形 plt.show() |
图形:
软件版本:
以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。如有错误或未考虑完全的地方,望不吝赐教。
原文链接:https://www.cnblogs.com/shanger/p/13202146.html