-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathBk3_Ch6_01.py
107 lines (83 loc) · 2.43 KB
/
Bk3_Ch6_01.py
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
###############
# Authored by Weisheng Jiang
# Book 3 | From Basic Arithmetic to Machine Learning
# Published and copyrighted by Tsinghua University Press
# Beijing, China, 2022
###############
# Bk3_Ch6_01
import math
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
def plot_surf(xx,yy,zz,caption):
norm_plt = plt.Normalize(zz.min(), zz.max())
colors = cm.RdYlBu_r(norm_plt(zz))
fig = plt.figure()
ax = fig.add_subplot(projection='3d')
# ax = plt.axes(projection='3d')
surf = ax.plot_surface(xx,yy,zz,
facecolors=colors, shade=False)
surf.set_facecolor((0,0,0,0))
plt.show()
ax.set_proj_type('ortho')
if xx.min() == xx.max():
ax.set_xlim(xx.min() - 4,xx.min() + 4)
else:
ax.set_xlim(xx.min(),xx.max())
if yy.min() == yy.max():
ax.set_ylim(yy.min() - 4,yy.min() + 4)
else:
ax.set_ylim(yy.min(),yy.max())
if zz.min() == zz.max():
ax.set_zlim(zz.min() - 4,zz.min() + 4)
else:
ax.set_zlim(zz.min(),zz.max())
plt.tight_layout()
ax.set_xlabel('$\it{x}$')
ax.set_ylabel('$\it{y}$')
ax.set_zlabel('$\it{z}$')
ax.set_title(caption)
ax.view_init(azim=-135, elev=30)
ax.xaxis._axinfo["grid"].update({"linewidth":0.25, "linestyle" : ":"})
ax.yaxis._axinfo["grid"].update({"linewidth":0.25, "linestyle" : ":"})
ax.zaxis._axinfo["grid"].update({"linewidth":0.25, "linestyle" : ":"})
num = 33
x = np.linspace(-4,4,num)
y = np.linspace(-4,4,num)
xx,yy = np.meshgrid(x,y);
plt.close('all')
#%% z - 2 = 0
zz = 2 + xx*0;
caption = '$z - 2 = 0$';
plot_surf (xx,yy,zz,caption)
#%% y - z = 0
zz = yy;
caption = '$z - y = 0$';
plot_surf (xx,yy,zz,caption)
#%% x - z = 0
zz = xx;
caption = '$x - z = 0$';
plot_surf (xx,yy,zz,caption)
#%% x + y - z = 0
zz = xx + yy;
caption = '$x + y - z = 0$';
plot_surf (xx,yy,zz,caption)
#%% vertical mesh plot
x = np.linspace(-4,4,num)
z = np.linspace(-4,4,num)
xx,zz = np.meshgrid(x,z);
#%% y - 2 = 0
yy = 2 - xx*0
caption = '$y - 2 = 0$';
plot_surf (xx,yy,zz,caption)
#%% x + y - 2 = 0
yy = 2 - xx
caption = '$x + y - 2 = 0$';
plot_surf (xx,yy,zz,caption)
#%% x + 2 = 0
y = np.linspace(-4,4,num)
z = np.linspace(-4,4,num)
yy,zz = np.meshgrid(y,z);
xx = -2 - yy*0
caption = '$x + 2 = 0$';
plot_surf (xx,yy,zz,caption)