@@ -116,11 +116,31 @@ def test_contour_manual_labels(split_collections):
116116
117117 plt .figure (figsize = (6 , 2 ), dpi = 200 )
118118 cs = plt .contour (x , y , z )
119+
120+ _maybe_split_collections (split_collections )
121+
119122 pts = np .array ([(1.0 , 3.0 ), (1.0 , 4.4 ), (1.0 , 6.0 )])
120123 plt .clabel (cs , manual = pts )
121124 pts = np .array ([(2.0 , 3.0 ), (2.0 , 4.4 ), (2.0 , 6.0 )])
122125 plt .clabel (cs , manual = pts , fontsize = 'small' , colors = ('r' , 'g' ))
123126
127+
128+ @pytest .mark .parametrize ("split_collections" , [False , True ])
129+ @image_comparison (['contour_disconnected_segments' ],
130+ remove_text = True , style = 'mpl20' , extensions = ['png' ])
131+ def test_contour_label_with_disconnected_segments (split_collections ):
132+ x , y = np .mgrid [- 1 :1 :21j , - 1 :1 :21j ]
133+ z = 1 / np .sqrt (0.01 + (x + 0.3 ) ** 2 + y ** 2 )
134+ z += 1 / np .sqrt (0.01 + (x - 0.3 ) ** 2 + y ** 2 )
135+
136+ plt .figure ()
137+ cs = plt .contour (x , y , z , levels = [7 ])
138+
139+ # Adding labels should invalidate the old style
140+ _maybe_split_collections (split_collections )
141+
142+ cs .clabel (manual = [(0.2 , 0.1 )])
143+
124144 _maybe_split_collections (split_collections )
125145
126146
@@ -232,6 +252,9 @@ def test_labels(split_collections):
232252 disp_units = [(216 , 177 ), (359 , 290 ), (521 , 406 )]
233253 data_units = [(- 2 , .5 ), (0 , - 1.5 ), (2.8 , 1 )]
234254
255+ # Adding labels should invalidate the old style
256+ _maybe_split_collections (split_collections )
257+
235258 CS .clabel ()
236259
237260 for x , y in data_units :
@@ -338,6 +361,22 @@ def test_clabel_zorder(use_clabeltext, contour_zorder, clabel_zorder):
338361 assert clabel .get_zorder () == expected_clabel_zorder
339362
340363
364+ def test_clabel_with_large_spacing ():
365+ # When the inline spacing is large relative to the contour, it may cause the
366+ # entire contour to be removed. In current implementation, one line segment is
367+ # retained between the identified points.
368+ # This behavior may be worth reconsidering, but check to be sure we do not produce
369+ # an invalid path, which results in an error at clabel call time.
370+ # see gh-27045 for more information
371+ x = y = np .arange (- 3.0 , 3.01 , 0.05 )
372+ X , Y = np .meshgrid (x , y )
373+ Z = np .exp (- X ** 2 - Y ** 2 )
374+
375+ fig , ax = plt .subplots ()
376+ contourset = ax .contour (X , Y , Z , levels = [0.01 , 0.2 , .5 , .8 ])
377+ ax .clabel (contourset , inline_spacing = 100 )
378+
379+
341380# tol because ticks happen to fall on pixel boundaries so small
342381# floating point changes in tick location flip which pixel gets
343382# the tick.
0 commit comments