@@ -3154,13 +3154,13 @@ def stem(self, *args, linefmt=None, markerfmt=None, basefmt=None, bottom=0,
3154
3154
self .add_container (stem_container )
3155
3155
return stem_container
3156
3156
3157
- @_api .make_keyword_only ("3.9 " , "explode " )
3157
+ @_api .rename_parameter ("3.11 " , "pctdistance" , "autodistance " )
3158
3158
@_preprocess_data (replace_names = ["x" , "explode" , "labels" , "colors" ])
3159
- def pie (self , x , explode = None , labels = None , colors = None ,
3160
- autopct = None , pctdistance = 0.6 , shadow = False , labeldistance = 1.1 ,
3159
+ def pie (self , x , * , explode = None , labels = None , colors = None , absolutefmt = None ,
3160
+ autopct = None , autodistance = 0.6 , shadow = False , labeldistance = 1.1 ,
3161
3161
startangle = 0 , radius = 1 , counterclock = True ,
3162
3162
wedgeprops = None , textprops = None , center = (0 , 0 ),
3163
- frame = False , rotatelabels = False , * , normalize = True , hatch = None ):
3163
+ frame = False , rotatelabels = False , normalize = True , hatch = None ):
3164
3164
"""
3165
3165
Plot a pie chart.
3166
3166
@@ -3193,18 +3193,38 @@ def pie(self, x, explode=None, labels=None, colors=None,
3193
3193
3194
3194
.. versionadded:: 3.7
3195
3195
3196
+ absolutefmt : None or str or callable, default: None
3197
+ If not *None*, *absolutefmt* is a string or function used to label
3198
+ the wedges with their original numeric values. The label will be
3199
+ placed according to *autodistance*. If *absolutefmt* is a format
3200
+ string, the label will be ``fmt % number``. If *absolutefmt* is a
3201
+ function, then it will be called. Cannot be used at the same time
3202
+ as *autopct*.
3203
+
3204
+ .. versionadded:: 3.11
3205
+
3196
3206
autopct : None or str or callable, default: None
3197
3207
If not *None*, *autopct* is a string or function used to label the
3198
- wedges with their numeric value. The label will be placed inside
3199
- the wedge. If *autopct* is a format string, the label will be
3200
- ``fmt % pct``. If *autopct* is a function, then it will be called.
3208
+ wedges with their percentage values. The label will be placed
3209
+ according to *autodistance*. If *autopct* is a format string, the
3210
+ label will be ``fmt % pct``. If *autopct* is a function, then it
3211
+ will be called. Cannot be used at the same time as *absolutefmt*.
3212
+
3213
+ autodistance : float, default: 0.6
3214
+ The relative distance along the radius at which the text
3215
+ generated by *absolutefmt* or *autopct* is drawn. To draw the text
3216
+ outside the pie, set *autodistance* > 1. This parameter is ignored
3217
+ if both *absolutefmt* and *autopct* are ``None``.
3201
3218
3202
3219
pctdistance : float, default: 0.6
3203
3220
The relative distance along the radius at which the text
3204
3221
generated by *autopct* is drawn. To draw the text outside the pie,
3205
3222
set *pctdistance* > 1. This parameter is ignored if *autopct* is
3206
3223
``None``.
3207
3224
3225
+ .. deprecated:: 3.11
3226
+ Use *autodistance* instead.
3227
+
3208
3228
labeldistance : float or None, default: 1.1
3209
3229
The relative distance along the radius at which the labels are
3210
3230
drawn. To draw the labels inside the pie, set *labeldistance* < 1.
@@ -3275,9 +3295,7 @@ def pie(self, x, explode=None, labels=None, colors=None,
3275
3295
The Axes aspect ratio can be controlled with `.Axes.set_aspect`.
3276
3296
"""
3277
3297
self .set_aspect ('equal' )
3278
- # The use of float32 is "historical", but can't be changed without
3279
- # regenerating the test baselines.
3280
- x = np .asarray (x , np .float32 )
3298
+ x = np .asarray (x )
3281
3299
if x .ndim > 1 :
3282
3300
raise ValueError ("x must be 1D" )
3283
3301
@@ -3287,9 +3305,11 @@ def pie(self, x, explode=None, labels=None, colors=None,
3287
3305
sx = x .sum ()
3288
3306
3289
3307
if normalize :
3290
- x = x / sx
3308
+ fracs = x / sx
3291
3309
elif sx > 1 :
3292
3310
raise ValueError ('Cannot plot an unnormalized pie with sum(x) > 1' )
3311
+ else :
3312
+ fracs = x
3293
3313
if labels is None :
3294
3314
labels = ['' ] * len (x )
3295
3315
if explode is None :
@@ -3324,7 +3344,7 @@ def get_next_color():
3324
3344
slices = []
3325
3345
autotexts = []
3326
3346
3327
- for frac , label , expl in zip (x , labels , explode ):
3347
+ for n , frac , label , expl in zip (x , fracs , labels , explode ):
3328
3348
x , y = center
3329
3349
theta2 = (theta1 + frac ) if counterclock else (theta1 - frac )
3330
3350
thetam = 2 * np .pi * 0.5 * (theta1 + theta2 )
@@ -3369,15 +3389,33 @@ def get_next_color():
3369
3389
texts .append (t )
3370
3390
3371
3391
if autopct is not None :
3372
- xt = x + pctdistance * radius * math . cos ( thetam )
3373
- yt = y + pctdistance * radius * math . sin ( thetam )
3392
+ if absolutefmt is not None :
3393
+ raise ValueError ( 'Only one of autopct and absolutefmt may be used.' )
3374
3394
if isinstance (autopct , str ):
3375
3395
s = autopct % (100. * frac )
3376
3396
elif callable (autopct ):
3377
3397
s = autopct (100. * frac )
3378
3398
else :
3379
3399
raise TypeError (
3380
3400
'autopct must be callable or a format string' )
3401
+ autolabels = True
3402
+
3403
+ elif absolutefmt is not None :
3404
+ if isinstance (absolutefmt , str ):
3405
+ s = absolutefmt % n
3406
+ elif callable (absolutefmt ):
3407
+ s = absolutefmt (n )
3408
+ else :
3409
+ raise TypeError (
3410
+ 'absolutefmt must be callable or a format string' )
3411
+ autolabels = True
3412
+
3413
+ else :
3414
+ autolabels = False
3415
+
3416
+ if autolabels :
3417
+ xt = x + autodistance * radius * math .cos (thetam )
3418
+ yt = y + autodistance * radius * math .sin (thetam )
3381
3419
if mpl ._val_or_rc (textprops .get ("usetex" ), "text.usetex" ):
3382
3420
# escape % (i.e. \%) if it is not already escaped
3383
3421
s = re .sub (r"([^\\])%" , r"\1\\%" , s )
@@ -3397,7 +3435,7 @@ def get_next_color():
3397
3435
xlim = (- 1.25 + center [0 ], 1.25 + center [0 ]),
3398
3436
ylim = (- 1.25 + center [1 ], 1.25 + center [1 ]))
3399
3437
3400
- if autopct is None :
3438
+ if not autotexts :
3401
3439
return slices , texts
3402
3440
else :
3403
3441
return slices , texts , autotexts
0 commit comments