25
25
26
26
def plotsurf (node , face , * args , ** kwargs ):
27
27
from mpl_toolkits .mplot3d .art3d import Poly3DCollection
28
+ from matplotlib .colors import Normalize
28
29
29
30
rngstate = np .random .get_state ()
30
31
h = []
@@ -43,9 +44,14 @@ def plotsurf(node, face, *args, **kwargs):
43
44
plt .figure () # Create a new figure
44
45
ax = plt .gcf ().add_subplot (projection = "3d" ) # Add 3D axes to the current figure
45
46
47
+ if not "color" in kwargs and not "cmap" in kwargs :
48
+ kwargs ["cmap" ] = plt .get_cmap ("jet" )
49
+
46
50
if isinstance (face , list ): # polyhedral facets
47
51
48
52
newsurf = {}
53
+ colormap = []
54
+
49
55
for fc in face :
50
56
if (
51
57
isinstance (fc , (list , tuple ))
@@ -64,7 +70,21 @@ def plotsurf(node, face, *args, **kwargs):
64
70
for subface in newsurf .values ()
65
71
for subf in subface
66
72
]
67
- colormap = [sc [i - 1 , :] for i , subface in newsurf .items () for subf in subface ]
73
+ if node .shape [1 ] > 3 :
74
+ node_values = node [:, 3 ]
75
+ face_values = np .array (
76
+ [
77
+ np .mean (node_values [np .array (subf ).flatten ()])
78
+ for subface in newsurf .values ()
79
+ for subf in subface
80
+ ]
81
+ )
82
+ norm = Normalize (vmin = face_values .min (), vmax = face_values .max ())
83
+ colormap = kwargs ["cmap" ](norm (face_values ))
84
+ else :
85
+ colormap = [
86
+ sc [i - 1 , :] for i , subface in newsurf .items () for subf in subface
87
+ ]
68
88
69
89
elif face .shape [1 ] == 2 :
70
90
h = plotedges (node , face , * args , ** kwargs )
@@ -90,27 +110,17 @@ def plotsurf(node, face, *args, **kwargs):
90
110
else :
91
111
polydata , colormap = plotasurf (node , face , * args , ** kwargs )
92
112
93
- if node .shape [1 ] > 3 and not "color" in kwargs and not "facecolors" in kwargs :
94
- colormap = []
95
- maxval = np .max (node [:, 3 ])
96
- minval = np .min (node [:, 3 ])
97
-
98
- for tri in polydata :
99
- val = node [tri , 3 ]
100
- val = np .mean (val )
101
- face_color = kwargs ["cmap" ](
102
- (val - minval ) / (maxval - minval )
103
- ) # Normalize for colormap
104
- colormap .append (face_color )
105
-
106
113
if "colormap" in locals () and len (colormap ) > 0 and not "facecolors" in kwargs :
107
114
kwargs ["facecolors" ] = colormap
108
- print (len (colormap ))
109
115
110
- if not "color" in kwargs and not "cmap" in kwargs :
111
- kwargs ["cmap" ] = plt .get_cmap ("jet" )
116
+ if "cmap" in kwargs and not "facecolors" in kwargs :
117
+ node_values = node [:, 3 ] if node .shape [1 ] > 3 else node [:, 2 ]
118
+ face_values = np .array ([np .mean (node_values [f ]) for f in face [:, :3 ] - 1 ])
119
+ norm = Normalize (vmin = face_values .min (), vmax = face_values .max ())
120
+ kwargs ["facecolors" ] = kwargs ["cmap" ](norm (face_values ))
112
121
113
122
patch = Poly3DCollection (polydata , edgecolors = "k" , ** kwargs )
123
+
114
124
ax .add_collection3d (patch )
115
125
_autoscale_3d (ax , node )
116
126
h .append (patch )
@@ -125,23 +135,13 @@ def plotsurf(node, face, *args, **kwargs):
125
135
126
136
127
137
def plotasurf (node , face , * args , ** kwargs ):
128
- poly3d = [[node [i , :3 ] for i in p ] for p in face [:, :3 ] - 1 ]
129
- colmap = []
130
- if node .shape [1 ] > 3 and not "color" in kwargs and not "facecolors" in kwargs :
131
- maxval = np .max (node [:, 3 ])
132
- minval = np .min (node [:, 3 ])
133
-
134
- for tri in face :
135
- val = node [tri , 3 ]
136
- val = np .mean (val )
137
- face_color = kwargs ["cmap" ](
138
- (val - minval ) / (maxval - minval )
139
- ) # Normalize for colormap
140
- colmap .append (face_color )
141
-
142
- if not "facecolors" in kwargs :
143
- kwargs ["facecolors" ] = colmap
138
+ from matplotlib .colors import Normalize
144
139
140
+ poly3d = [[node [i , :3 ] for i in p ] for p in face [:, :3 ] - 1 ]
141
+ node_values = node [:, 3 ] if node .shape [1 ] > 3 else node [:, 2 ]
142
+ face_values = np .array ([np .mean (node_values [f ]) for f in face [:, :3 ] - 1 ])
143
+ norm = Normalize (vmin = face_values .min (), vmax = face_values .max ())
144
+ colmap = kwargs ["cmap" ](norm (face_values ))
145
145
return poly3d , colmap
146
146
147
147
@@ -164,6 +164,7 @@ def plottetra(node, elem, *args, **kwargs):
164
164
"""
165
165
166
166
from mpl_toolkits .mplot3d .art3d import Poly3DCollection
167
+ from matplotlib .colors import Normalize
167
168
168
169
# Save current RNG state
169
170
rngstate = np .random .get_state ()
@@ -182,6 +183,9 @@ def plottetra(node, elem, *args, **kwargs):
182
183
plt .figure () # Create a new figure
183
184
ax = plt .gcf ().add_subplot (projection = "3d" ) # Add 3D axes to the current figure
184
185
186
+ if not "color" in kwargs and not "cmap" in kwargs :
187
+ kwargs ["cmap" ] = plt .get_cmap ("jet" )
188
+
185
189
h = []
186
190
polydata = []
187
191
colormap = []
@@ -205,14 +209,15 @@ def plottetra(node, elem, *args, **kwargs):
205
209
if "colormap" in locals () and len (colormap ) > 0 and not "facecolors" in kwargs :
206
210
kwargs ["facecolors" ] = colormap
207
211
208
- if not "color " in kwargs and not "cmap " in kwargs :
209
- kwargs [ "cmap" ] = plt . get_cmap ( "jet" )
210
- print ( "set cmap to jet" )
211
-
212
- print ( kwargs . keys ( ))
212
+ if "cmap " in kwargs and not "facecolors " in kwargs :
213
+ node_values = node [:, 3 ] if node . shape [ 1 ] > 3 else node [:, 2 ]
214
+ face_values = np . array ([ np . mean ( node_values [ f ]) for f in elem [:, : 4 ] - 1 ] )
215
+ norm = Normalize ( vmin = face_values . min (), vmax = face_values . max ())
216
+ kwargs [ "facecolors" ] = kwargs [ "cmap" ]( norm ( face_values ))
213
217
214
218
patch = Poly3DCollection (polydata , edgecolors = "k" , ** kwargs )
215
219
ax .add_collection3d (patch )
220
+
216
221
_autoscale_3d (ax , node )
217
222
h .append (patch )
218
223
0 commit comments