Open
Description
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
Matplotlib Backend
No response
Python version
No response
Jupyter version
No response
Installation
git checkout