-
-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Closed
Description
Summary
In the Axes.grouped_bar() implementation, the code currently passes repetitive keyword arguments separately for vertical and horizontal orientations.
Tim Hoffmann suggested a more maintainable approach by introducing a shared common_kwargs dictionary to consolidate repeated parameters such as align, label, color, and hatch.
Current Code Snippet::
if orientation == "vertical":
bc = self.bar(lefts, hs, width=bar_width, align="edge",
label=label, color=color, hatch=hatch_pattern, **kwargs)
else:
bc = self.barh(lefts, hs, height=bar_width, align="edge",
label=label, color=color, hatch=hatch_pattern, **kwargs)
Proposed Refactor::
common_kwargs = dict(align="edge", label=label, color=color, hatch=hatch_pattern)
if orientation == "vertical":
bc = self.bar(lefts, hs, width=bar_width, **common_kwargs, **kwargs)
else:
bc = self.barh(lefts, hs, height=bar_width, **common_kwargs, **kwargs)
Benefits
Eliminates redundant code across orientation branches.
Reduces likelihood of introducing inconsistencies or errors during future updates.
Makes the code cleaner and easier to maintain.
Removes the need for a separate orientation-specific test since the logic path becomes symmetrical.
Reference:
Suggestion from @timhoffm in PR ENH: Add broadcasted hatch to grouped_bar #30683
Proposed fix
common_kwargs = dict(align="edge", label=label, color=color, hatch=hatch_pattern)
if orientation == "vertical":
bc = self.bar(lefts, hs, width=bar_width, **common_kwargs, **kwargs)
else:
bc = self.barh(lefts, hs, height=bar_width, **common_kwargs, **kwargs)