本文仅仅梳理最基本的绘图方法。
一、初始化
假设已经安装了matplotlib工具包。
利用matplotlib.figure.Figure创建一个图框:
1
2
3
4
importmatplotlib.pyplot as plt
frommpl_toolkits.mplot3dimportAxes3D
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
importmatplotlib as mpl
frommpl_toolkits.mplot3dimportAxes3D
importnumpy as np
importmatplotlib.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)
z=np.linspace(-2,2,100)
r=z**2+1
x=r*np.sin(theta)
y=r*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
frommpl_toolkits.mplot3dimportAxes3D
importmatplotlib.pyplot as plt
importnumpy as np
defrandrange(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')
n=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].
forc, m, zlow, zhighin[('r','o',-50,-25), ('b','^',-30,-5)]:
xs=randrange(n,23,32)
ys=randrange(n,0,100)
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
frommpl_toolkits.mplot3dimportaxes3d
importmatplotlib.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
frommpl_toolkits.mplot3dimportAxes3D
importmatplotlib.pyplot as plt
frommatplotlibimportcm
frommatplotlib.tickerimportLinearLocator, FormatStrFormatter
importnumpy as np
fig=plt.figure()
ax=fig.gca(projection='3d')
# Make data.
X=np.arange(-5,5,0.25)
Y=np.arange(-5,5,0.25)
X, Y=np.meshgrid(X, Y)
R=np.sqrt(X**2+Y**2)
Z=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.01,1.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学习大礼包,完全免费分享给每一位渴望成长、希望突破自我现状却略感迷茫的朋友。无论您是编程新手还是希望深化技能的开发者,都欢迎加入我们的学习之旅,共同交流进步!
🌟 学习大礼包包含内容:
-
Python全领域学习路线图:一目了然,指引您从基础到进阶,再到专业领域的每一步学习路径,明确各方向的核心知识点。
-
超百节Python精品视频课程:涵盖Python编程的必备基础知识、高效爬虫技术、以及深入的数据分析技能,让您技能全面升级。
-
实战案例集锦:精选超过100个实战项目案例,从理论到实践,让您在解决实际问题的过程中,深化理解,提升编程能力。
-
华为独家Python漫画教程:创新学习方式,以轻松幽默的漫画形式,让您随时随地,利用碎片时间也能高效学习Python。
-
互联网企业Python面试真题集:精选历年知名互联网企业面试真题,助您提前备战,面试准备更充分,职场晋升更顺利。
👉 立即领取方式:只需【点击这里】,即刻解锁您的Python学习新篇章!让我们携手并进,在编程的海洋里探索无限可能!



被折叠的 条评论
为什么被折叠?



