@@ -1129,6 +1129,74 @@ def set_paths(self, patches):
1129
1129
for p in patches ]
1130
1130
self ._paths = paths
1131
1131
1132
+ class TriMesh (Collection ):
1133
+ """
1134
+ Class for the efficient drawing of a triangular mesh using
1135
+ Gouraud shading.
1136
+
1137
+ A triangular mesh is a :class:`~matplotlib.tri.Triangulation`
1138
+ object.
1139
+ """
1140
+ def __init__ (self , triangulation , ** kwargs ):
1141
+ Collection .__init__ (self , ** kwargs )
1142
+ self ._triangulation = triangulation ;
1143
+ self ._shading = 'gouraud'
1144
+ self ._is_filled = True
1145
+
1146
+ self ._bbox = transforms .Bbox .unit ()
1147
+
1148
+ # Unfortunately this requires a copy, unless Triangulation
1149
+ # was rewritten.
1150
+ xy = np .hstack ((triangulation .x .reshape (- 1 ,1 ),
1151
+ triangulation .y .reshape (- 1 ,1 )))
1152
+ self ._bbox .update_from_data_xy (xy )
1153
+
1154
+ def get_paths (self ):
1155
+ if self ._paths is None :
1156
+ self .set_paths ()
1157
+ return self ._paths
1158
+
1159
+ def set_paths (self ):
1160
+ self ._paths = self .convert_mesh_to_paths (self ._triangulation )
1161
+
1162
+ @staticmethod
1163
+ def convert_mesh_to_paths (tri ):
1164
+ """
1165
+ Converts a given mesh into a sequence of
1166
+ :class:`matplotlib.path.Path` objects for easier rendering by
1167
+ backends that do not directly support meshes.
1168
+
1169
+ This function is primarily of use to backend implementers.
1170
+ """
1171
+ Path = mpath .Path
1172
+ triangles = tri .get_masked_triangles ()
1173
+ verts = np .concatenate ((tri .x [triangles ][...,np .newaxis ],
1174
+ tri .y [triangles ][...,np .newaxis ]), axis = 2 )
1175
+ return [Path (x ) for x in verts ]
1176
+
1177
+ @allow_rasterization
1178
+ def draw (self , renderer ):
1179
+ if not self .get_visible (): return
1180
+ renderer .open_group (self .__class__ .__name__ )
1181
+ transform = self .get_transform ()
1182
+
1183
+ # Get a list of triangles and the color at each vertex.
1184
+ tri = self ._triangulation
1185
+ triangles = tri .get_masked_triangles ()
1186
+
1187
+ verts = np .concatenate ((tri .x [triangles ][...,np .newaxis ],
1188
+ tri .y [triangles ][...,np .newaxis ]), axis = 2 )
1189
+
1190
+ self .update_scalarmappable ()
1191
+ colors = self ._facecolors [triangles ];
1192
+
1193
+ gc = renderer .new_gc ()
1194
+ self ._set_gc_clip (gc )
1195
+ gc .set_linewidth (self .get_linewidth ()[0 ])
1196
+ renderer .draw_gouraud_triangles (gc , verts , colors , transform .frozen ())
1197
+ gc .restore ()
1198
+ renderer .close_group (self .__class__ .__name__ )
1199
+
1132
1200
1133
1201
class QuadMesh (Collection ):
1134
1202
"""
@@ -1315,7 +1383,7 @@ def draw(self, renderer):
1315
1383
1316
1384
1317
1385
patchstr = artist .kwdoc (Collection )
1318
- for k in ('QuadMesh' , 'PolyCollection' , 'BrokenBarHCollection' ,
1386
+ for k in ('QuadMesh' , 'TriMesh' , ' PolyCollection' , 'BrokenBarHCollection' ,
1319
1387
'RegularPolyCollection' , 'PathCollection' ,
1320
1388
'StarPolygonCollection' , 'PatchCollection' ,
1321
1389
'CircleCollection' , 'Collection' ,):
0 commit comments