Skip to content

[Bug]: boxplot errors when x and tick_labels are empty #32304

Open

Description

@carlosgmartin

Bug summary

boxplot errors when the arguments x and tick_labels are both empty.

Code for reproduction

from matplotlib import pyplot as plt

plt.boxplot([], tick_labels=[])

Actual outcome

Traceback (most recent call last):
  File "<string>", line 1, in <module>
    from matplotlib import pyplot as plt; plt.boxplot([], tick_labels=[])
                                          ~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^
  File "/venv/lib/python3.14/site-packages/matplotlib/_api/deprecation.py", line 477, in wrapper
    return func(*args, **kwargs)
  File "/venv/lib/python3.14/site-packages/matplotlib/pyplot.py", line 3234, in boxplot
    return gca().boxplot(
           ~~~~~~~~~~~~~^
        x,
        ^^
    ...<28 lines>...
        **({"data": data} if data is not None else {}),
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/venv/lib/python3.14/site-packages/matplotlib/_api/deprecation.py", line 477, in wrapper
    return func(*args, **kwargs)
  File "/venv/lib/python3.14/site-packages/matplotlib/__init__.py", line 1528, in inner
    return func(
        ax,
        *map(cbook.sanitize_sequence, args),
        **{k: cbook.sanitize_sequence(v) for k, v in kwargs.items()})
  File "/venv/lib/python3.14/site-packages/matplotlib/axes/_axes.py", line 4541, in boxplot
    bxpstats = cbook.boxplot_stats(x, whis=whis, bootstrap=bootstrap,
                                   labels=tick_labels, autorange=autorange)
  File "/venv/lib/python3.14/site-packages/matplotlib/cbook.py", line 1269, in boxplot_stats
    raise ValueError("Dimensions of labels and X must be compatible")
ValueError: Dimensions of labels and X must be compatible

Expected outcome

Drawing nothing, without crashing.

Additional information

The error is raised here.

This happens because _reshape_2D([], "X") is returning [[]], which has a len of 1 rather than 0.

Operating system

macOS Tahoe 26.5.2

Matplotlib Version

3.11.1

Matplotlib Backend

macosx

Python version

Python 3.14.6

Jupyter version

7.6.0

Installation

pip

Activity

  1. iccir commented on Sep 5, 2026

    @iccir
    Contributor

    The documentation for tick_labels is:

    Ticks are always placed at the box positions

    And for positions:

    The positions of the boxes. The ticks and limits are
    automatically set to match the positions. Defaults to
    range(1, N+1) where N is the number of boxes to be drawn.

    So it's expected that tick_labels and positions will line up. This also fails, although with a different error message:

    plt.boxplot([], positions=[])
    # ValueError: List of boxplot statistics and `positions` values must have same the length

    I'm not sure what the proper behavior or fix should be.

  2. rcomer commented on Sep 12, 2026

    @rcomer
    Member

    Hi @carlosgmartin, could you say something about your use case and how you came to be passing these both as empty lists? I can imagine a workflow where the dataset X has an unknown number of data points, and sometimes zero data points. However, intuitively that dataset would still have a label associated with it so I'm unclear how you get the empty list of labels. I note that these both work fine

    plt.boxplot([])
    plt.boxplot([], tick_labels=['foo'])
  3. rcomer commented on Sep 12, 2026

    @rcomer
    Member

    Actually I guess it could happen if you are appending an unknown number of data sets to your list, and sometimes zero data sets. In that case the user code could simply not call boxplot when the list is empty. If there are no data sets then there is nothing to do. Unfortunately the case

    plt.boxplot([])

    is ambigous: it could be one empty data set, or it could be zero data sets. The current Matplotlib behaviour seems to assume it is one empty data set.

  4. carlosgmartin commented on Sep 12, 2026

    @carlosgmartin
    ContributorAuthor

    @rcomer My use case is handling N datasets, where sometimes N = 0.

    Skipping the call entirely when N = 0 is a workaround, but not user-friendly.

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