本期速覽:
目錄
11、3D圖
11.1 3D散點(diǎn)圖
11.2 3D散點(diǎn)圖設(shè)置長(zhǎng)寬比
11.3 3D折線圖
11.4 3D圖設(shè)置視角
11.5 3D三角形曲面圖
11.6 3D等高線圖
11.7 3D等高線圖個(gè)性化colormap
11.8 更科學(xué)的3D等高線圖方法
11.9 3D線框圖
11.10 3D曲面圖
11.11 3D曲面圖指定cstride和rstride值
11.12 3D球
11.13 修改3D球視角
12、2D等高線圖
12.1 默認(rèn)2D等高線圖
12.2 個(gè)性化2D等高線圖
11、3D圖
該部分通過(guò)3D圖介紹Matplotlib使用。
11.1 3D散點(diǎn)圖
N = 250
np.random.seed(124)
x = 15 * np.random.random(N)
y = np.sin(x) + 0.25 * np.random.random(N)
z = np.cos(x) + 0.25 * np.random.random(N)
plt.figure(figsize=(6, 6), dpi=110)
ax = plt.axes(projection='3d') #開(kāi)啟3D模式
ax.scatter3D(x, y, z, color='r') #3D散點(diǎn)圖
ax.set_xlabel('x', fontsize=20, labelpad=20)
ax.set_ylabel('y', fontsize=20, labelpad=20)
ax.set_zlabel('z', fontsize=20, labelpad=20)
11.2 3D散點(diǎn)圖設(shè)置長(zhǎng)寬比
N = 250
np.random.seed(124)
x = 15 * np.random.random(N)
y = np.sin(x) + 0.25 * np.random.random(N)
z = np.cos(x) + 0.25 * np.random.random(N)
plt.figure(figsize=(6, 6), dpi=110)
ax = plt.axes(projection='3d') #開(kāi)啟3D模式
ax.scatter3D(x, y, z, color='r') #3D散點(diǎn)圖
ax.set_xlabel('x', fontsize=20, labelpad=20)
ax.set_ylabel('y', fontsize=20, labelpad=20)
ax.set_zlabel('z', fontsize=20, labelpad=20)
ax.set_box_aspect((2., 1.5, 1.2)) #設(shè)置長(zhǎng)寬比
11.3 3D折線圖
N = 100
np.random.seed(124)
xline = np.linspace(0, 15, N)
yline = np.sin(xline)
zline = np.cos(xline)
fig = plt.figure(figsize=(9, 6), dpi=110)
ax = plt.axes(projection='3d')
ax.plot3D(xline, yline, zline) #plot3D繪制折線圖
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
ax.set_box_aspect((2, 1.5, 1.2))
11.4 3D圖設(shè)置視角
N = 250
np.random.seed(124)
x = 15 * np.random.random(N)
y = np.sin(x) + 0.25 * np.random.random(N)
z = np.cos(x) + 0.25 * np.random.random(N)
plt.figure(figsize=(6, 6), dpi=110)
ax = plt.axes(projection='3d')
ax.scatter3D(x, y, z, color='r')
ax.set_xlabel('x', fontsize=20, labelpad=20)
ax.set_ylabel('y', fontsize=20, labelpad=20)
ax.set_zlabel('z', fontsize=20, labelpad=20)
ax.set_box_aspect((2., 1.5, 1.2))
ax.view_init(10, 180) #設(shè)置3D圖角度,10是仰角,180是方位角
更多案例,
11.5 3D三角形曲面圖
N = 2000
np.random.seed(124)
r = 2 * np.pi * np.random.random(N)
theta = 20 * np.pi * np.random.random(N)
xdata = np.ravel(r * np.sin(theta))
ydata = np.ravel(r * np.cos(theta))
zdata = np.sin(xdata) + np.cos(ydata)
fig = plt.figure(figsize=(15, 6), dpi=110)
plt.subplots_adjust(wspace=0)
ax1 = plt.subplot(121, projection='3d')
ax1.plot_trisurf(xdata, ydata, zdata, cmap='inferno') #.plot_trisurf繪制3D曲面
ax1.set_xlabel('x')
ax1.set_ylabel('y')
ax1.set_zlabel('z')
ax1.view_init(40, 100)
ax1.set_box_aspect((4.5, 4.5, 1.5))
ax1.set_title('Elevation = 40$^\circ$, Azimuth = 100$^\circ)
ax2 = plt.subplot(122, projection='3d')
ax2.plot_trisurf(xdata, ydata, zdata, cmap='inferno')
ax2.set_xlabel('x')
ax2.set_ylabel('y')
ax2.set_zlabel('z')
ax2.view_init(20, 100)
ax2.set_box_aspect((4.5, 4.5, 1.5))
ax2.set_title('Elevation = 20$^\circ$, Azimuth = 100$^\circ)
11.6 3D等高線圖
N = 100
np.random.seed(3124)
x = np.linspace(-2, 2, N) + np.random.random(N)
y = np.linspace(-2, 2, N) + np.random.random(N)
X, Y = np.meshgrid(x, y)
Z = np.sin(X) + np.cos(Y)
fig = plt.figure(figsize=(9, 6), dpi=110)
ax = plt.axes(projection='3d')
ax.contour3D(X, Y, Z, cmap='Spectral') #contour3D繪制等高線圖
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
ax.set_box_aspect((2, 2, 1))
ax.view_init(60, 100)
ax.set_title('Contour counts = Default, elevation = 60, azimuth = 100')
11.7 3D等高線圖個(gè)性化colormap
N = 100
np.random.seed(3124)
x = np.linspace(-2, 2, N) + np.random.random(N)
y = np.linspace(-2, 2, N) + np.random.random(N)
X, Y = np.meshgrid(x, y)
Z = np.sin(X) + np.cos(Y)
fig = plt.figure(figsize=(20, 6), dpi=110)
ax = plt.axes(projection='3d')
p = ax.contour3D(
X,
Y,
Z,
256,
cmap='Spectral' #個(gè)性化colormap
)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
ax.set_box_aspect((3, 3, 1))
ax.view_init(60, 100)
plt.colorbar(p)
ax.set_title('Contour counts = 256, elevation = 60, azimuth = 100')
11.8 更科學(xué)的3D等高線圖方法
N = 100
np.random.seed(3124)
x = np.linspace(-2, 2, N) + np.random.random(N)
y = np.linspace(-2, 2, N) + np.random.random(N)
X, Y = np.meshgrid(x, y)
Z = np.sin(X) + np.cos(Y)
plt.figure(figsize=(14, 6), dpi=110)
ax1 = plt.subplot(121, projection='3d')
ax1.contour(X, Y, Z, cmap='Spectral') #更好的3D等高線方法contour
ax1.set_xlabel('x')
ax1.set_ylabel('y')
ax1.set_zlabel('z')
ax1.set_box_aspect((3, 3, 1))
ax1.view_init(10, 100)
ax1.set_title('Contour Default, elevation = 10, azimuth = 100')
ax2 = plt.subplot(122, projection='3d')
ax2.contourf(X, Y, Z, cmap='Spectral') #更好的3D等高線方法contourf
ax2.set_xlabel('x')
ax2.set_ylabel('y')
ax2.set_zlabel('z')
ax2.set_box_aspect((3, 3, 1))
ax2.view_init(10, 100)
ax2.set_title('Contourf Default, elevation = 10, azimuth = 100')
11.9 3D線框圖
N = 100
np.random.seed(3124)
x = np.linspace(-2, 2, N) + np.random.random(N)
y = np.linspace(-2, 2, N) + np.random.random(N)
X, Y = np.meshgrid(x, y)
Z = np.sin(X) + np.cos(Y)
fig = plt.figure(figsize=(6, 6), dpi=110)
ax = plt.axes(projection='3d')
ax.plot_wireframe(X, Y, Z, color='k', alpha=.2) #plot_wireframe
11.10 3D曲面圖
N = 100
np.random.seed(3124)
x = np.linspace(-2, 2, N) + np.random.random(N)
y = np.linspace(-2, 2, N) + np.random.random(N)
X, Y = np.meshgrid(x, y)
Z = np.sin(X) + np.cos(Y)
fig = plt.figure(figsize=(8, 8), dpi=110)
ax = plt.axes(projection='3d') # 3d contour plot
ax.plot_surface(
X,
Y,
Z,
)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
ax.set_box_aspect((2, 2, 1))
ax.view_init(10, 100)
ax.set_title('Plot surface Default, elevation = 10, azimuth = 100')
11.11 3D曲面圖指定cstride和rstride值
N = 200
np.random.seed(3124)
x = np.linspace(-2, 2, N) + np.random.random(N)
y = np.linspace(-2, 2, N) + np.random.random(N)
X, Y = np.meshgrid(x, y)
Z = np.sin(X) + np.cos(Y)
fig = plt.figure(figsize=(14, 6))
ax1 = plt.subplot(121, projection='3d') # 3d contour plot
ax1.plot_surface(X, Y, Z, cmap='Spectral')
ax1.set_xlabel('x')
ax1.set_ylabel('y')
ax1.set_zlabel('z')
ax1.set_box_aspect((2, 2, 1))
ax1.view_init(60, 100)
ax1.set_title(
'Plot surface rstride = cstride = default, \n elevation = 60, azimuth = 100'
)
ax2 = plt.subplot(122, projection='3d') # 3d contour plot
ax2.plot_surface(X, Y, Z, cmap='Spectral', rstride=1,
cstride=1) #指定cstride和rstride的值
ax2.set_xlabel('x')
ax2.set_ylabel('y')
ax2.set_zlabel('z')
ax2.set_box_aspect((2, 2, 1))
ax2.view_init(60, 100)
ax2.set_title(
'Plot surface rstride = cstride = 1, \n elevation = 60, azimuth = 100')
11.12 3D球
u = np.linspace(0, 2 * np.pi, 100)
v = np.linspace(0, np.pi, 100)
x = 10 * np.outer(np.cos(u), np.sin(v))
y = 10 * np.outer(np.sin(u), np.sin(v))
z = 10 * np.outer(np.ones(np.size(u)), np.cos(v))
plt.figure(figsize=(10, 10))
ax = plt.subplot(projection='3d')
ax.plot_surface(x, y, z, cmap='inferno') #plot_surface
11.13 修改3D球視角
u = np.linspace(0, 2 * np.pi, 100)
v = np.linspace(0, np.pi, 100)
x = 10 * np.outer(np.cos(u), np.sin(v))
y = 10 * np.outer(np.sin(u), np.sin(v))
z = 10 * np.outer(np.ones(np.size(u)), np.cos(v))
rows = 2
columns = 2
grid = plt.GridSpec(rows, columns, wspace=.2, hspace=.2)
elev = np.arange(0, 40, 10)
azim = np.arange(0, 200, 50)
plt.figure(figsize=(12, 12))
for i in range(rows * columns):
ax = plt.subplot(grid[i], projection='3d')
ax.plot_surface(x, y, z, cmap='inferno')
ax.view_init(elev[i], azim[i]) #修改視角
ax.set_title('Elevation = ' + str(elev[i]) + ', Azimuth = ' + str(azim[i]))
12、2D等高線圖
該部分通過(guò)2D等高線圖學(xué)習(xí)Matplotlib。
12.1 默認(rèn)2D等高線圖
N = 100
np.random.seed(100)
x = np.linspace(-2, 2, N) + np.random.random(N)
y = np.linspace(-2, 2, N) + np.random.random(N)
X, Y = np.meshgrid(x, y)
Z = np.sin(X) + np.cos(Y)
plt.figure(figsize=(7, 5), dpi=110)
plt.contour(X, Y, Z) #contour
plt.title('Contour 2D Default', pad=10)
12.2 個(gè)性化2D等高線圖
N = 100
np.random.seed(100)
x = np.linspace(-2, 2, N) + np.random.random(N)
y = np.linspace(-2, 2, N) + np.random.random(N)
X, Y = np.meshgrid(x, y)
Z = np.sin(X) + np.cos(Y)
plt.figure(figsize=(15, 5))
plt.subplot(121)
plt.contour(X, Y, Z, 256)
plt.title('Contour 2D counts = 256, cmap = viridis', pad=10)
plt.colorbar()
plt.subplot(122)
plt.contour(X, Y, Z, 256, cmap='Spectral')
plt.colorbar()
plt.title('Contour 2D counts = 256, cmap = Spectral', pad=10)
N = 100
np.random.seed(100)
x = np.linspace(-2, 2, N) + np.random.random(N)
y = np.linspace(-2, 2, N) + np.random.random(N)
X, Y = np.meshgrid(x, y)
Z = np.sin(X) + np.cos(Y)**3
plt.figure(figsize=(15, 5))
plt.subplot(121)
plt.contourf(X, Y, Z, 50, cmap = 'inferno')
plt.colorbar()
plt.title('Contourf 2D counts = 50', pad = 10)
plt.subplot(122)
plt.contourf(X, Y, Z, 200, cmap = 'inferno')
plt.colorbar()
plt.title('Contourf 2D counts = 200', pad = 10)