Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 33dacc7

Browse files
submit code
1 parent 6be6594 commit 33dacc7

File tree

6 files changed

+152
-1
lines changed

6 files changed

+152
-1
lines changed

‎chaoxi/Matplotlib_3D/Matplotlib_line.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
from mpl_toolkits.mplot3d import Axes3D
4+
5+
# 依次获取画布和绘图区并创建 Axes3D 对象
6+
fig = plt.figure()
7+
ax = fig.gca(projection='3d')
8+
9+
# 第一条3D线性图数据
10+
theta = np.linspace(-4 * np.pi, 4 * np.pi, 100)
11+
z1 = np.linspace(-2, 2, 100)
12+
r = z1**2 + 1
13+
x1 = r * np.sin(theta)
14+
y1 = r * np.cos(theta)
15+
16+
# 第二条3D线性图数据
17+
z2 = np.linspace(-3, 3, 100)
18+
x2 = np.sin(z2)
19+
y2 = np.cos(z2)
20+
21+
# 绘制3D线性图
22+
ax.plot(x1, y1, z1, color='b', label='3D Line1')
23+
ax.plot(x2, y2, z2, color='r', label='3D Line2')
24+
25+
# 设置标题、轴标签、图例,也可以直接使用 plt.title、plt.xlabel、plt.legend...
26+
ax.set_title('3D Line View', pad=15, fontsize='10')
27+
ax.set_xlabel('x ', color='r', fontsize='14')
28+
ax.set_ylabel('y ', color='g', fontsize='14')
29+
ax.set_zlabel('z ', color='b', fontsize='14')
30+
ax.legend()
31+
plt.show()

‎chaoxi/Matplotlib_3D/matplotlib_3d.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import matplotlib.pyplot as plt
2+
from matplotlib import cm
3+
from matplotlib.ticker import LinearLocator, FormatStrFormatter
4+
from mpl_toolkits.mplot3d import Axes3D
5+
import numpy as np
6+
7+
fig = plt.figure()
8+
9+
# 指定图形类型是 3d 类型
10+
ax = fig.add_subplot(projection='3d')
11+
12+
# 构造数据
13+
X = np.arange(-5, 5, 0.25)
14+
Y = np.arange(-5, 5, 0.25)
15+
X, Y = np.meshgrid(X, Y)
16+
R = np.sqrt(X**2 + Y**2)
17+
Z = np.sin(R)
18+
19+
# Plot the surface.
20+
surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm,
21+
linewidth=0, antialiased=False)
22+
# Customize the z axis.
23+
ax.set_zlim(-1.01, 1.01)
24+
ax.zaxis.set_major_locator(LinearLocator(10))
25+
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))
26+
# Add a color bar which maps values to colors.
27+
fig.colorbar(surf, shrink=0.5, aspect=5)
28+
29+
plt.show()
30+
31+
32+
33+
34+

‎chaoxi/Matplotlib_3D/matplotlib_3d1.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
from mpl_toolkits.mplot3d import Axes3D
4+
5+
fig = plt.figure()
6+
# 指定图形类型为 3d 类型
7+
ax = fig.add_subplot(111, projection='3d')
8+
# X, Y value
9+
X = np.arange(-5, 5, 0.25)
10+
Y = np.arange(-5, 5, 0.25)
11+
12+
# 设置 x-y 平面的网格
13+
X, Y = np.meshgrid(X, Y)
14+
R = np.sqrt(X ** 2 + Y ** 2)
15+
# height value
16+
Z = np.sin(R)
17+
18+
# rstride:行之间的跨度 cstride:列之间的跨度
19+
# rcount:设置间隔个数,默认50个,ccount:列的间隔个数 不能与上面两个参数同时出现
20+
#vmax和vmin 颜色的最大值和最小值
21+
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=plt.get_cmap('rainbow'))
22+
23+
# zdir : 'z' | 'x' | 'y' 表示把等高线图投射到哪个面
24+
# offset : 表示等高线图投射到指定页面的某个刻度
25+
ax.contourf(X,Y,Z,zdir='z',offset=-2)
26+
27+
# 设置图像z轴的显示范围,x、y轴设置方式相同
28+
ax.set_zlim(-2,2)
29+
30+
plt.show()
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import matplotlib.pyplot as plt
2+
import numpy as np
3+
from mpl_toolkits.mplot3d import Axes3D
4+
5+
def randrange(n, vmin, vmax):
6+
7+
return (vmax - vmin) * np.random.rand(n) + vmin
8+
9+
fig = plt.figure()
10+
ax = fig.add_subplot(111, projection='3d')
11+
12+
n = 100
13+
14+
# For each set of style and range settings, plot n random points in the box
15+
# defined by x in [23, 32], y in [0, 100], z in [zlow, zhigh].
16+
for c, m, zlow, zhigh in [('r', 'o', -50, -25), ('b', '^', -30, -5)]:
17+
xs = randrange(n, 23, 32)
18+
ys = randrange(n, 0, 100)
19+
zs = randrange(n, zlow, zhigh)
20+
ax.scatter(xs, ys, zs, c=c, marker=m)
21+
22+
ax.set_title('3D Diagram View', pad=15, fontsize='10')
23+
ax.set_xlabel('x ', color='r', fontsize='14')
24+
ax.set_ylabel('y ', color='g', fontsize='14')
25+
ax.set_zlabel('z ', color='b', fontsize='14')
26+
27+
plt.show()

‎chaoxi/Matplotlib_3D/review_2d.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import matplotlib.path as mpath
2+
import matplotlib.patches as mpatches
3+
import matplotlib.pyplot as plt
4+
5+
fig, ax = plt.subplots()
6+
Path = mpath.Path
7+
# Path 控制坐标点绘制贝塞尔曲线
8+
# 图形数据构造
9+
path_data = [
10+
(Path.MOVETO, (1.88, -2.57)),
11+
(Path.CURVE4, (0.35, -1.1)),
12+
(Path.CURVE4, (-1.75, 1.5)),
13+
(Path.CURVE4, (0.375, 2.0)),
14+
(Path.LINETO, (0.85, 1.15)),
15+
(Path.CURVE4, (2.2, 3.2)),
16+
(Path.CURVE4, (3, 0.05)),
17+
(Path.CURVE4, (2.0, -1.5)),
18+
(Path.CLOSEPOLY, (1.58, -2.57)),
19+
]
20+
codes,verts = zip(*path_data)
21+
path = mpath.Path(verts, codes)
22+
patch = mpatches.PathPatch(path, facecolor='r', alpha=0.5)
23+
ax.add_patch(patch)
24+
# plot control points and connecting lines
25+
x, y = zip(*path.vertices)
26+
line, = ax.plot(x, y, 'go-')
27+
ax.grid()
28+
ax.axis('equal')
29+
plt.show()

‎chaoxi/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
+ [send_email](https://github.com/JustDoPython/python-examples/tree/master/chaoxi/send_email) :今天,我用 Python 给武汉人民发一封邮件
77
+ [Earth_view](https://github.com/JustDoPython/python-examples/tree/master/chaoxi/Earth_view) :如何用 Python 制作地球仪?
88
+ [twoai_chat](https://github.com/JustDoPython/python-examples/tree/master/chaoxi/twoai_chat) :两个机器人在一起会碰撞出怎样的的火花?
9+
+ [Matplotlib_3D](https://github.com/JustDoPython/python-examples/tree/master/chaoxi/Matplotlib_3D) :Python 30 行代码画各种 3D 图形
910

10-
twoai_chat
1111
---
1212

1313
从小白到工程师的学习之路

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /