Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Bug]: grouped_bar can pass label and color twice, causing TypeError #30739

@ilakkmanoharan

Description

@ilakkmanoharan

Bug summary

In the current implementation of Axes.grouped_bar, calling the function with label or color keyword arguments can result in a TypeError: matplotlib.axes._axes.Axes.bar() got multiple values for keyword argument 'label'.

This happens because grouped_bar explicitly passes label and color to self.bar(), while kwargs may also contain these keys if the caller provided them. Python then sees two values for the same keyword argument.

Example that triggers the error:

fig, ax = plt.subplots()
x = np.arange(2)
heights = [np.array([1, 2]), np.array([2, 3])]

ax.grouped_bar(
    heights,
    positions=x,
    label="dataset",
    color=["red", "blue"]
)
# TypeError: Axes.bar() got multiple values for keyword argument 'label'

Cause
Inside grouped_bar:

bc = self.bar(lefts, hs, width=bar_width, align="edge",
              label=label, color=color, **kwargs)

If kwargs also contains label or color, bar() receives the same argument twice.
This can occur:
When the caller passes label or color explicitly.
When a DataFrame or dict input automatically generates labels internally, and kwargs contains the same keys.

Code for reproduction

import numpy as np
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
x = np.arange(2)
heights = [np.array([1, 2]), np.array([2, 3])]

# This triggers the TypeError
ax.grouped_bar(
    heights,
    positions=x,
    label="dataset",
    color=["red", "blue"]
)

Actual outcome

TypeError: matplotlib.axes._axes.Axes.bar() got multiple values for keyword argument 'label'

Image

Expected outcome

A grouped bar plot with two groups, each group having two bars colored red and blue, and a legend showing "dataset" for the bars.

Additional information

This bug happens whenever label or color is explicitly passed in kwargs to grouped_bar.
It can also happen with a pandas DataFrame input if labels are automatically assigned internally.

Operating system

macOS

Matplotlib Version

3.11.0.dev1467+gdb83efff4.d20251111

Matplotlib Backend

macosx

Python version

3.13.7

Jupyter version

No response

Installation

pip

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions