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

Skip to content

[Bug]: Turning on minor gridlines in a multi-Axes Figure, created with subplots(), that contains >1 boxplot results in a ValueError #26484

Closed
@chupin10

Description

@chupin10

Bug summary

When creating multiple Axes with boxplots, all on the same Figure, adding minor gridlines using minorticks_on() causes a traceback only if more than 1 Axes has boxplots on it.

Code for reproduction

from random import random

import matplotlib.pyplot as plt


if __name__ == "__main__":
    fig, axes = plt.subplots(sharex=True, ncols=2, nrows=2)

    def values():
        return [random() for _ in range(9)]

    for x in range(3):
        """
        Code works as is, with commented lines not being run.
        When I uncomment any of the below boxplot() calls, I am getting the error
        mentioned in the bug report.
        """

        # axes[0, 0].boxplot(values(), positions=[x])
        # axes[0, 1].boxplot(values(), positions=[x])
        # axes[1, 0].boxplot(values(), positions=[x])
        axes[1, 1].boxplot(values(), positions=[x])

    for ax in axes.flatten():
        ax.grid(which="major", color="k", alpha=0.3)
        ax.grid(which="minor", color="k", alpha=0.1, linestyle="--")
        ax.minorticks_on()

    plt.show()

Actual outcome

When I run this code exactly as I've pasted into the code for repro box, I get a Figure with 4 Axes. Each of them has major and minor gridlines (expected/desired), and one of them contains a set of boxplots.

When I run this and attempt to create boxplots on a second Axes within the figure, I get the following traceback when the Figure is attempted to be rendered:

Traceback (most recent call last):
  File "python3.9/site-packages/matplotlib/backend_bases.py", line 1226, in _on_timer
    ret = func(*args, **kwargs)
  File "python3.9/site-packages/matplotlib/backends/backend_macosx.py", line 68, in callback_func
    callback()
  File "python3.9/site-packages/matplotlib/backends/backend_macosx.py", line 88, in _draw_idle
    self.draw()
  File "python3.9/site-packages/matplotlib/backends/backend_macosx.py", line 50, in draw
    super().draw()
  File "ython3.9/site-packages/matplotlib/backends/backend_agg.py", line 400, in draw
    self.figure.draw(self.renderer)
  File "python3.9/site-packages/matplotlib/artist.py", line 95, in draw_wrapper
    result = draw(artist, renderer, *args, **kwargs)
  File "python3.9/site-packages/matplotlib/artist.py", line 72, in draw_wrapper
    return draw(artist, renderer)
  File "python3.9/site-packages/matplotlib/figure.py", line 3140, in draw
    mimage._draw_list_compositing_images(
  File "python3.9/site-packages/matplotlib/image.py", line 131, in _draw_list_compositing_images
    a.draw(renderer)
  File "python3.9/site-packages/matplotlib/artist.py", line 72, in draw_wrapper
    return draw(artist, renderer)
  File "python3.9/site-packages/matplotlib/axes/_base.py", line 3028, in draw
    self._update_title_position(renderer)
  File "python3.9/site-packages/matplotlib/axes/_base.py", line 2963, in _update_title_position
    bb = ax.xaxis.get_tightbbox(renderer)
  File "python3.9/site-packages/matplotlib/axis.py", line 1323, in get_tightbbox
    ticks_to_draw = self._update_ticks()
  File "python3.9/site-packages/matplotlib/axis.py", line 1270, in _update_ticks
    minor_locs = self.get_minorticklocs()
  File "python3.9/site-packages/matplotlib/axis.py", line 1489, in get_minorticklocs
    minor_locs = np.asarray(self.minor.locator())
  File "python3.9/site-packages/matplotlib/ticker.py", line 2955, in __call__
    locs = np.arange(tmin, tmax, minorstep) + t0
ValueError: arange: cannot compute length`

In addition, the 3 commented out boxplot() calls can be made successfully without throwing this error at render-time if you don't call ax.minorticks_on(), however AFAIK this is the way to ensure the minor gridlines are shown.

Expected outcome

A Figure with 4 Axes, each of which containing a series of boxplots with major and minor gridlines drawn as well.

Additional information

Machine is a Mac with an Apple M1 CPU.

Operating system

OS/X

Matplotlib Version

3.7.1

Matplotlib Backend

MacOSX

Python version

3.9.6

Jupyter version

No response

Installation

pip

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions