@@ -494,9 +494,27 @@ def do_3d_projection(self, renderer):
494494
495495 ecs = (_zalpha (self ._edgecolor3d , vzs ) if self ._depthshade else
496496 self ._edgecolor3d )
497+
498+ # Sort the points based on z coordinates
499+ # Performance optimization: Create a sorted index array and reorder
500+ # points and point properties according to the index array
501+ z_markers_idx = np .argsort (vzs )[::- 1 ]
502+
503+ # Re-order items
504+ vzs = vzs [z_markers_idx ]
505+ vxs = vxs [z_markers_idx ]
506+ vys = vys [z_markers_idx ]
507+ fcs = fcs [z_markers_idx ]
508+ ecs = ecs [z_markers_idx ]
509+ vps = np .column_stack ((vxs , vys ))
510+
511+ fcs = mcolors .to_rgba_array (fcs , self ._alpha )
497512 ecs = mcolors .to_rgba_array (ecs , self ._alpha )
513+
498514 self .set_edgecolors (ecs )
499- PathCollection .set_offsets (self , np .column_stack ([vxs , vys ]))
515+ self .set_facecolors (fcs )
516+
517+ PathCollection .set_offsets (self , vps )
500518
501519 return np .min (vzs ) if vzs .size else np .nan
502520
@@ -663,17 +681,16 @@ def do_3d_projection(self, renderer):
663681 in enumerate (zip (xyzlist , cface , cedge ))),
664682 key = lambda x : x [0 ], reverse = True )
665683
666- segments_2d = [s for z , s , fc , ec , idx in z_segments_2d ]
684+ zzs , segments_2d , self ._facecolors2d , self ._edgecolors2d , idxs = \
685+ zip (* z_segments_2d )
686+
667687 if self ._codes3d is not None :
668- codes = [self ._codes3d [idx ] for z , s , fc , ec , idx in z_segments_2d ]
688+ codes = [self ._codes3d [idx ] for idx in idxs ]
669689 PolyCollection .set_verts_and_codes (self , segments_2d , codes )
670690 else :
671691 PolyCollection .set_verts (self , segments_2d , self ._closed )
672692
673- self ._facecolors2d = [fc for z , s , fc , ec , idx in z_segments_2d ]
674- if len (self ._edgecolors3d ) == len (cface ):
675- self ._edgecolors2d = [ec for z , s , fc , ec , idx in z_segments_2d ]
676- else :
693+ if len (self ._edgecolors3d ) != len (cface ):
677694 self ._edgecolors2d = self ._edgecolors3d
678695
679696 # Return zorder value
0 commit comments