Skip to content

[Bug]: offsetbox classes have optional arguments that are really not optional #24623

Closed
@oscargus

Description

@oscargus

Bug summary

In #24620 it was pointed out that "Also, not passing pad leads to an error, so it should probably be 0. as default rather than None."

This holds for many other classes in the offsetbox module as well.

Code for reproduction

import matplotlib.pyplot as plt
from matplotlib.offsetbox import PaddedBox, AnchoredText

fig, ax = plt.subplots()

at = AnchoredText("foo",  'upper left')
pb = PaddedBox(at, patch_attrs={'facecolor' : 'r'}, draw_frame=True)

ax.add_artist(pb)
fig.show()

Actual outcome

Traceback (most recent call last):
  File "C:\Users\Oscar\matplotlib\lib\matplotlib\backends\backend_qt.py", line 454, in _draw_idle
    self.draw()
  File "C:\Users\Oscar\matplotlib\lib\matplotlib\backends\backend_agg.py", line 400, in draw
    self.figure.draw(self.renderer)
  File "C:\Users\Oscar\matplotlib\lib\matplotlib\artist.py", line 74, in draw_wrapper
    result = draw(artist, renderer, *args, **kwargs)
  File "C:\Users\Oscar\matplotlib\lib\matplotlib\artist.py", line 51, in draw_wrapper
    return draw(artist, renderer)
  File "C:\Users\Oscar\matplotlib\lib\matplotlib\figure.py", line 3070, in draw
    mimage._draw_list_compositing_images(
  File "C:\Users\Oscar\matplotlib\lib\matplotlib\image.py", line 131, in _draw_list_compositing_images
    a.draw(renderer)
  File "C:\Users\Oscar\matplotlib\lib\matplotlib\artist.py", line 51, in draw_wrapper
    return draw(artist, renderer)
  File "C:\Users\Oscar\matplotlib\lib\matplotlib\axes\_base.py", line 3157, in draw
    mimage._draw_list_compositing_images(
  File "C:\Users\Oscar\matplotlib\lib\matplotlib\image.py", line 131, in _draw_list_compositing_images
    a.draw(renderer)
  File "C:\Users\Oscar\matplotlib\lib\matplotlib\offsetbox.py", line 544, in draw
    w, h, xdescent, ydescent, offsets = self.get_extent_offsets(renderer)
  File "C:\Users\Oscar\matplotlib\lib\matplotlib\offsetbox.py", line 537, in get_extent_offsets
    pad = self.pad * dpicor
TypeError: unsupported operand type(s) for *: 'NoneType' and 'float'

Expected outcome

No exception. A sensible default value provided.

Additional information

This seems to hold for many pad parameters, but there are others as well. For example, for the packers, I think all numerical arguments must have a value, although they are listed as optional, so None will be used for computations unless provided.

Operating system

No response

Matplotlib Version

main

Matplotlib Backend

No response

Python version

No response

Jupyter version

No response

Installation

git checkout

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions