Skip to content

[Bug]: Unable to use single Quantity for xerr in ax.errorbar #29630

Open
@geolta

Description

@geolta

Bug summary

When using units in matplotlib (in this case pint: https://pint.readthedocs.io/en/stable/) it is not possible to use a single Quantity (magnitude + unit) to specify a single xerr or yerr in ax.errorbar.

Code for reproduction

### WORKING ###
###############

import numpy as np
from matplotlib import pyplot as plt

x = np.array([1, 2, 3])
y = np.array([3, 1.5, 1])

fig, ax = plt.subplots()
ax.errorbar(x, y, yerr=0.5)
plt.show()

### NOT WORKING ###
###################

import numpy as np
import pint
from matplotlib import pyplot as plt

ureg = pint.UnitRegistry()
ureg.setup_matplotlib()

x = np.array([1, 2, 3]) * ureg.meter
y = np.array([3, 1.5, 1]) * ureg.second

fig, ax = plt.subplots()
ax.errorbar(x, y, yerr=ureg.Quantity(0.5, "second"))
plt.show()

Actual outcome

/home/user/.local/lib/python3.12/site-packages/matplotlib/axes/_axes.py:3638: UnitStrippedWarning: The unit of the quantity is stripped when downcasting to ndarray.
  x = np.asarray(x, dtype=object)
/home/user/.local/lib/python3.12/site-packages/matplotlib/axes/_axes.py:3640: UnitStrippedWarning: The unit of the quantity is stripped when downcasting to ndarray.
  y = np.asarray(y, dtype=object)

Expected outcome

No warning / error.

Additional information

No response

Operating system

Fedora 40

Matplotlib Version

3.10.0

Matplotlib Backend

qtagg

Python version

3.12.8

Jupyter version

No response

Installation

pip

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