MEP28: Remove Complexity from Axes.boxplot
Exposing cbook.boxplot_stats through Axes.boxplot kwargs: None
Remove redundant statistical kwargs in Axes.boxplot: None
Remove redundant style options in Axes.boxplot: None
Remaining items that arise through discussion: None
seed
(
0
)
fig
,
ax
=
plt
.
subplots
(
figsize
=
(
4
,
6
))
ax
.
set_yscale
(
'log'
)
data
=
np
.
random
.
lognormal
(
-
1.75
,
2.75
,
size
=
37
)
stats
=
cbook
.
boxplot_stats
(
data
,
labels
=
[
'arithmetic'
])
logstats
=
cbook
.
boxplot_stats
(
np
.
log
(
data
),
labels
=
[
'log-transformed'
])
for
lsdict
in
logstats
:
for
key
,
value
in
lsdict
.
items
():
if
key
!=
'label'
:
lsdict
[
key
]
=
np
.
exp
(
value
)
stats
.
extend
(
logstats
)
ax
.
bxp
(
stats
)
fig
.
show
()
sym - processed by 12 SLOC, 4 if blocks
Removing the sym option allows all code in handling the remaining
styling parameters to be moved to Axes.bxp. This doesn't remove
any complexity, but does reinforce the single responsibility principle
among Axes.bxp, cbook.boxplot_stats, and Axes.boxplot.
Additionally, the notch parameter could be renamed shownotches
to be consistent with Axes.bxp. This kind of cleanup could be taken
a step further and the whis, bootstrap, autorange could
be rolled into the kwargs passed to the new statfxn parameter.
x
:
x
output
=
[]
for
_d
in
data
:
d
=
transform_in
(
_d
)
stat_dict
=
do_stats
(
d
)
for
key
,
value
in
stat_dict
.
item
():
if
key
!=
'label'
:
stat_dict
[
key
]
=
transform_out
(
value
)
output
.
append
(
d
)
return
output
class
Axes
(
...
):
def
boxplot_option1
(
data
,
...
,
transform_in
=
None
,
transform_out
=
None
):
stats
=
cbook
.
boxplot_stats
(
data
,
...
,
transform_in
=
transform_in
,
transform_out
=
transform_out
)
return
self
.
bxp
(
stats
,
...
)
def
boxplot_option2
(
data
,
...
,
statfxn
=
None
,
**
statopts
):
if
statfxn
is
None
:
statfxn
=
boxplot_stats
stats
=
statfxn
(
data
,
**
statopts
)
return
self
.
bxp
(
stats
,
...
)
palette
=
"PRGn"
,
shownotches
=
True
,
statfxn
=
my_box_stats
,
bootstrap_method
=
'BCA'
,
whisker_method
=
'dynamic'
)