-
Notifications
You must be signed in to change notification settings - Fork 6
/
FillBetween3d.py
67 lines (42 loc) · 2.15 KB
/
FillBetween3d.py
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# -*- coding: utf-8 -*-
"""
Created on Fri Jul 12 14:04:23 2019
@author: artmenlope
"""
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
def fill_between_3d(ax,x1,y1,z1,x2,y2,z2,mode=1,c='steelblue',alpha=0.6):
"""
Function similar to the matplotlib.pyplot.fill_between function but
for 3D plots.
input:
ax -> The axis where the function will plot.
x1 -> 1D array. x coordinates of the first line.
y1 -> 1D array. y coordinates of the first line.
z1 -> 1D array. z coordinates of the first line.
x2 -> 1D array. x coordinates of the second line.
y2 -> 1D array. y coordinates of the second line.
z2 -> 1D array. z coordinates of the second line.
modes:
mode = 1 -> Fill between the lines using the shortest distance between
both. Makes a lot of single trapezoids in the diagonals
between lines and then adds them into a single collection.
mode = 2 -> Uses the lines as the edges of one only 3d polygon.
Other parameters (for matplotlib):
c -> the color of the polygon collection.
alpha -> transparency of the polygon collection.
"""
if mode == 1:
for i in range(len(x1)-1):
verts = [(x1[i],y1[i],z1[i]), (x1[i+1],y1[i+1],z1[i+1])] + \
[(x2[i+1],y2[i+1],z2[i+1]), (x2[i],y2[i],z2[i])]
ax.add_collection3d(Poly3DCollection([verts],
alpha=alpha,
linewidths=0,
color=c))
if mode == 2:
verts = [(x1[i],y1[i],z1[i]) for i in range(len(x1))] + \
[(x2[i],y2[i],z2[i]) for i in range(len(x2))]
ax.add_collection3d(Poly3DCollection([verts],alpha=alpha,color=c))