Skip to content

[Bug]: clabel manual argument does not accept units #27525

Closed

Description

@QuLogic

Bug summary

As found in #27490, while contour does appear to allow units, if you pass manual label locations to the manual argument of `clabel, it will fail.

Code for reproduction

import datetime

import numpy as np
import matplotlib.pyplot as plt


# Sample data for contour plot
dates = [datetime.datetime(2023, 10, 1) + datetime.timedelta(days=i) for i in range(10)]
x_start, x_end, x_step = -10.0, 5.0, 0.5
y_start, y_end, y_step = 0, 10, 1

x = np.arange(x_start, x_end, x_step)
y = np.arange(y_start, y_end, y_step)

# In this case, Y axis has dates
X, Y = np.meshgrid(x, dates)

rows = len(X)
cols = len(X[0])

z1D = np.arange(rows * cols)
Z = z1D.reshape((rows, cols))

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

ax.clabel(CS, CS.levels, inline=True, fmt=dict(zip(CS.levels, dates)),
          manual=[(x, y) for x, y in zip(x, dates)])

Actual outcome

Traceback (most recent call last):
  File "/home/elliott/code/matplotlib/clabel.py", line 27, in <module>
    ax.clabel(CS, CS.levels, inline=True, fmt=dict(zip(CS.levels, dates)),
  File "/home/elliott/code/matplotlib/lib/matplotlib/axes/_axes.py", line 6581, in clabel
    return CS.clabel(levels, **kwargs)
  File "/home/elliott/code/matplotlib/lib/matplotlib/contour.py", line 195, in clabel
    self.add_label_near(x, y, inline, inline_spacing)
  File "/home/elliott/code/matplotlib/lib/matplotlib/contour.py", line 553, in add_label_near
    x, y = transform.transform((x, y))
  File "/home/elliott/code/matplotlib/lib/matplotlib/transforms.py", line 1508, in transform
    res = self.transform_affine(self.transform_non_affine(values))
  File "/home/elliott/code/matplotlib/lib/matplotlib/_api/deprecation.py", line 297, in wrapper
    return func(*args, **kwargs)
  File "/home/elliott/code/matplotlib/lib/matplotlib/transforms.py", line 2422, in transform_affine
    return self.get_affine().transform(values)
  File "/home/elliott/code/matplotlib/lib/matplotlib/transforms.py", line 1797, in transform
    return self.transform_affine(values)
  File "/home/elliott/code/matplotlib/lib/matplotlib/_api/deprecation.py", line 297, in wrapper
    return func(*args, **kwargs)
  File "/home/elliott/code/matplotlib/lib/matplotlib/transforms.py", line 1868, in transform_affine
    return affine_transform(values, mtx)
TypeError: Cannot cast array data from dtype('O') to dtype('float64') according to the rule 'safe'

Expected outcome

Labels are added at spots corresponding to those given in manual.

Additional information

No response

Operating system

No response

Matplotlib Version

589d3fb

Matplotlib Backend

No response

Python version

No response

Jupyter version

No response

Installation

git checkout

Activity

  1. aman-coder03 commented on Feb 12, 2026

    @aman-coder03
    Contributor

    Hi @QuLogic
    i would be interested in looking into this.
    From the traceback, it seems like the manual label coordinates might not be going through unit conversion before being passed to the transform. Does that sound like the right place to start investigating?
    Happy to put together a small fix and corresponding test if that’s the intended direction.

  2. QuLogic commented on Mar 11, 2026

    @QuLogic
    MemberAuthor

    @aman-coder03 Yes, I'd say that might be the right place to start looking.

  3. aman-coder03 commented on Mar 11, 2026

    @aman-coder03
    Contributor

    hey @QuLogic i have opened a PR for this... #31278

  4. added this to the v3.10.9 milestone on Mar 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions