mirror of
https://github.com/Estom/notes.git
synced 2026-02-04 02:53:57 +08:00
49 lines
1.4 KiB
Markdown
49 lines
1.4 KiB
Markdown
# 三维曲面(棋盘格)
|
|
|
|
演示绘制以棋盘图案着色的3D表面。
|
|
|
|

|
|
|
|
```python
|
|
# This import registers the 3D projection, but is otherwise unused.
|
|
from mpl_toolkits.mplot3d import Axes3D # noqa: F401 unused import
|
|
|
|
import matplotlib.pyplot as plt
|
|
from matplotlib.ticker import LinearLocator
|
|
import numpy as np
|
|
|
|
|
|
fig = plt.figure()
|
|
ax = fig.gca(projection='3d')
|
|
|
|
# Make data.
|
|
X = np.arange(-5, 5, 0.25)
|
|
xlen = len(X)
|
|
Y = np.arange(-5, 5, 0.25)
|
|
ylen = len(Y)
|
|
X, Y = np.meshgrid(X, Y)
|
|
R = np.sqrt(X**2 + Y**2)
|
|
Z = np.sin(R)
|
|
|
|
# Create an empty array of strings with the same shape as the meshgrid, and
|
|
# populate it with two colors in a checkerboard pattern.
|
|
colortuple = ('y', 'b')
|
|
colors = np.empty(X.shape, dtype=str)
|
|
for y in range(ylen):
|
|
for x in range(xlen):
|
|
colors[x, y] = colortuple[(x + y) % len(colortuple)]
|
|
|
|
# Plot the surface with face colors taken from the array we made.
|
|
surf = ax.plot_surface(X, Y, Z, facecolors=colors, linewidth=0)
|
|
|
|
# Customize the z axis.
|
|
ax.set_zlim(-1, 1)
|
|
ax.w_zaxis.set_major_locator(LinearLocator(6))
|
|
|
|
plt.show()
|
|
```
|
|
|
|
## 下载这个示例
|
|
|
|
- [下载python源码: surface3d_3.py](https://matplotlib.org/_downloads/surface3d_3.py)
|
|
- [下载Jupyter notebook: surface3d_3.ipynb](https://matplotlib.org/_downloads/surface3d_3.ipynb) |