ax.fill_between如何在3d图中绘制
时间: 2024-03-22 18:39:11 浏览: 134
要在3D图中绘制ax.fill_between,需要使用mpl_toolkits.mplot3d.art3d.Poly3DCollection类。具体步骤如下:
1.导入必要的库:
```python
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
```
2.创建3D图:
```python
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
```
3.创建x、y、z的值:
```python
x = [1, 2, 3, 4, 5]
y = [1, 2, 3, 4, 5]
z1 = [0, 1, 0, 1, 0]
z2 = [1, 0, 1, 0, 1]
```
4.使用ax.fill_between绘制填充区域:
```python
verts = [(x[i], y[i], z1[i]) for i in range(len(x))] + [(x[i], y[i], z2[i]) for i in range(len(x)-1,-1,-1)]
poly = Poly3DCollection([verts], alpha=.25, facecolor='red', edgecolor='black')
ax.add_collection3d(poly)
```
完整代码如下:
```python
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x = [1, 2, 3, 4, 5]
y = [1, 2, 3, 4, 5]
z1 = [0, 1, 0, 1, 0]
z2 = [1, 0, 1, 0, 1]
verts = [(x[i], y[i], z1[i]) for i in range(len(x))] + [(x[i], y[i], z2[i]) for i in range(len(x)-1,-1,-1)]
poly = Poly3DCollection([verts], alpha=.25, facecolor='red', edgecolor='black')
ax.add_collection3d(poly)
plt.show()
```
这样,就可以在3D图中绘制填充区域了。
阅读全文