Skip to content

[Bug]: contour levels with units are not converted correctly #25063

Open
@dstansby

Description

@dstansby

Bug summary

When trying to pass levels with unit-ful data to contour, the units are not correctly converted. In the example below I choose 7 contour levels from min(Z) to max(Z), and convert the units from kg to g. This should just change the units, not the locations where contour levels are drawn. The levels array is interpreted without units, and no contours are drawn.

Code for reproduction

import matplotlib.pyplot as plt
import numpy as np

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

X, Y = np.meshgrid(np.linspace(-3, 3, 16), np.linspace(-3, 3, 16))
Z = (1 - X/2 + X**5 + Y**3) * np.exp(-X**2 - Y**2)
Z *= ureg.kg
levels = np.linspace(np.min(Z), np.max(Z), 7).to(ureg.g)

fig, ax = plt.subplots()
ax.contour(X, Y, Z, levels=levels)
plt.show()

Actual outcome

blank

Expected outcome

correct

Additional information

No response

Operating system

No response

Matplotlib Version

3.7.0.dev1431+g526cb71e20.d20230123

Matplotlib Backend

No response

Python version

No response

Jupyter version

No response

Installation

None

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