@@ -131,31 +131,20 @@ def _clip_mpl(self, gc, vertices, obj):
131131 [bbox .x0 , bbox .y1 ],
132132 ]
133133
134+ # Remove any NaN values from the vertices
135+ vertices = vertices [~ np .isnan (vertices ).any (axis = 1 )]
136+
134137 if obj == "patch" :
135138 vertices = ClippingRect2d (cliprect [0 ], cliprect [2 ]).clip_polyline (
136139 vertices
137140 )
138141 elif obj == "line2d" :
139142 cliprect = Polygon (cliprect )
140143 line = LineString (vertices )
141- try :
142- intersection = line .intersection (cliprect )
143- except Exception :
144- # Fix plotted values with large gaps/jumps crashing the intersection function
145- gap_threshold = 50
146- smoothed_vertices = [vertices [0 ]] # Start with the first point
147- for i in range (1 , len (vertices )):
148- if (
149- abs (vertices [i ][0 ] - vertices [i - 1 ][0 ]) <= gap_threshold
150- and abs (vertices [i ][1 ] - vertices [i - 1 ][1 ])
151- <= gap_threshold
152- ):
153- smoothed_vertices .append (vertices [i ])
154- line = LineString (smoothed_vertices )
155- intersection = line .intersection (cliprect )
156-
157- # Check if intersection is a multi-part geometry
144+ intersection = line .intersection (cliprect )
145+
158146 if intersection .is_empty :
147+ # if intersection is empty, return empty list of vertices
159148 vertices = [] # No intersection
160149 elif (
161150 "Multi" in intersection .geom_type
@@ -180,33 +169,33 @@ def _draw_mpl_lwpoly(self, gc, path, transform, obj):
180169 vertices = path .transformed (transform ).vertices
181170
182171 # clip the polygon if clip rectangle present
172+ if len (vertices ) > 0 :
173+ if isinstance (vertices [0 ][0 ], float or np .float64 ):
174+ vertices = self ._clip_mpl (gc , vertices , obj = obj )
183175
184- if isinstance ( vertices [ 0 ][ 0 ], float or np . float64 ) :
185- vertices = self ._clip_mpl (gc , vertices , obj = obj )
176+ else :
177+ vertices = [ self ._clip_mpl (gc , points , obj = obj ) for points in vertices ]
186178
187- else :
188- vertices = [self ._clip_mpl (gc , points , obj = obj ) for points in vertices ]
179+ # if vertices.
180+ if len (vertices ) == 0 :
181+ entity = None
189182
190- # if vertices.
191- if len (vertices ) == 0 :
192- entity = None
183+ else :
184+ if isinstance (vertices [0 ][0 ], float or np .float64 ):
185+ if vertices [0 ][0 ] != 0 :
186+ entity = self .modelspace .add_lwpolyline (
187+ points = vertices , close = False , dxfattribs = dxfattribs
188+ ) # set close to false because it broke some arrows
189+ else :
190+ entity = None
193191
194- else :
195- if isinstance (vertices [0 ][0 ], float or np .float64 ):
196- if vertices [0 ][0 ] != 0 :
197- entity = self .modelspace .add_lwpolyline (
198- points = vertices , close = False , dxfattribs = dxfattribs
199- ) # set close to false because it broke some arrows
200192 else :
201- entity = None
202-
203- else :
204- entity = [
205- self .modelspace .add_lwpolyline (
206- points = points , close = False , dxfattribs = dxfattribs
207- )
208- for points in vertices
209- ] # set close to false because it broke some arrows
193+ entity = [
194+ self .modelspace .add_lwpolyline (
195+ points = points , close = False , dxfattribs = dxfattribs
196+ )
197+ for points in vertices
198+ ] # set close to false because it broke some arrows
210199 return entity
211200
212201 def _draw_mpl_line2d (self , gc , path , transform ):
@@ -335,7 +324,7 @@ def draw_path_collection(
335324 offset_position ,
336325 ):
337326 for i , path in enumerate (paths ):
338- if all_transforms :
327+ if len ( all_transforms ) :
339328 combined_transform = master_transform + all_transforms [i ]
340329 else :
341330 combined_transform = master_transform
0 commit comments