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