Skip to content

[MNT]: Refactor grouped_bar orientation logic using shared common_kwargs #30729

@ilakkmanoharan

Description

@ilakkmanoharan

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)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions