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

Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
QuLogic opened this issue Dec 16, 2023 · 0 comments
Open

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

QuLogic opened this issue Dec 16, 2023 · 0 comments

Comments

@QuLogic
Copy link
Member

QuLogic commented Dec 16, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant