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

Skip to content

[Bug]: Secondary axis does not accept python builtins for transform #22473

@ClementPhan

Description

@ClementPhan

Bug summary

The creation of a secondary axis is documented to accept a tuple of functions to create the transform between the two axes.

When created, it passes a numpy array to the declared functions, which may not accept the Type.

Code for reproduction

from matplotlib import pyplot as plt
from math import degrees, radians, pi

def deg2rad(x: float) -> float: return x/180*pi
def rad2deg(x: float) -> float: return x/pi*180

fig, ax = plt.subplots()

ax.plot([0, 1], [1, 0])
ax.secondary_xaxis('top', functions=(degrees, radians)) # Actual output
# ax.secondary_xaxis('top', functions=(deg2rad, rad2deg)) # Expected output

plt.show()

Actual outcome

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\X\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1892, in __call__
    return self.func(*args)
  File "C:\Users\X\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 814, in callit
    func(*args)
  File "C:\Users\X\Desktop\temp\.venv\lib\site-packages\matplotlib\backends\_backend_tk.py", line 240, in idle_draw
    self.draw()
  File "C:\Users\X\Desktop\temp\.venv\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 9, in draw
    super().draw()
  File "C:\Users\X\Desktop\temp\.venv\lib\site-packages\matplotlib\backends\backend_agg.py", line 406, in draw
    self.figure.draw(self.renderer)
  File "C:\Users\X\Desktop\temp\.venv\lib\site-packages\matplotlib\artist.py", line 74, in draw_wrapper
    result = draw(artist, renderer, *args, **kwargs)
  File "C:\Users\X\Desktop\temp\.venv\lib\site-packages\matplotlib\artist.py", line 51, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "C:\Users\X\Desktop\temp\.venv\lib\site-packages\matplotlib\figure.py", line 2776, in draw
    artists = self._get_draw_artists(renderer)
  File "C:\Users\X\Desktop\temp\.venv\lib\site-packages\matplotlib\figure.py", line 247, in _get_draw_artists
    child.apply_aspect(pos)
  File "C:\Users\X\Desktop\temp\.venv\lib\site-packages\matplotlib\axes\_secondary_axes.py", line 123, in apply_aspect
    self._set_lims()
  File "C:\Users\X\Desktop\temp\.venv\lib\site-packages\matplotlib\axes\_secondary_axes.py", line 234, in _set_lims
    lims = self._functions[0](np.array(lims))
TypeError: only size-1 arrays can be converted to Python scalars

Expected outcome

Figure_1

Additional information

Line 234 in _secondary_axes.py lims = self._functions[0](np.array(lims)) passes a numpy array to a function that may accept only floats.

Operating system

Windows

Matplotlib Version

3.5.1

Matplotlib Backend

TkAgg

Python version

3.9.2

Jupyter version

N/A

Installation

pip

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