Skip to content

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

Closed

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)

Activity

  1. story645 commented on Nov 5, 2025

    @story645
    Member

    These sorta pure implementation questions should mostly be handled in the pull request as part of the pull request discussion.

    The main reasons to open an issue are if the implementation discussion:

    • Goes out of scope for the pull request
    • Gets very large and unwieldy, in effect also moving out of scope
    • Is a major API/behavior change.
  2. timhoffm commented on Nov 5, 2025

    @timhoffm
    Member

    This is a small internal code change. Please do it directly in the PR. You are touching that code anyway. (Or don't and argue in the comment why it should not be done).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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