-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Description
Bug summary
Right now, it looks like the type hints for Axes.annotate only accept a tuple of floats as input for the xy
and xycoords
arguments, but that is too narrow. Specifically, if I've plotted something with a datetime on the x axis (a not-unusual practice), I am most likely going to specify the annotation position with an acceptable datetime-like object.
There are no runtime issues, but both mypy and basedpyright complain about the annotate
line in the following code (ignore the messy output, it's just for example purposes). t
is truly a pd.Timestamp
, but even if I try using a native datetime
object the problem occurs.
Code for reproduction
from typing import cast
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
idx = pd.date_range(start='01/01/2014', end='03/01/2014', freq='1h')
n = len(idx)
rng = np.random.default_rng()
data = rng.normal(size=n)
df = pd.DataFrame({"data": data}, index=idx)
daily = df.resample("D")
maxs = daily.max()
mins = daily.min()
ax = maxs.plot()
mins.plot(ax=ax)
for t, x in maxs["data"].items():
t = cast(pd.Timestamp, t)
x = cast(float, x)
ax.annotate(str(x), (t, x))
plt.show()
Actual outcome
$ basedpyright dummy1.py
/home/andrea/Code/playground/dummy1.py
/home/andrea/Code/playground/dummy1.py:26:26 - error: Argument of type "tuple[Timestamp, float]" cannot be assigned to parameter "xy" of type "tuple[float, float]" in function "annotate"
"Timestamp" is not assignable to "float" (reportArgumentType)
1 error, 0 warnings, 0 notes
$ mypy dummy1.py
dummy1.py:26: error: Argument 2 to "annotate" of "Axes" has incompatible type "tuple[Timestamp, float]"; expected "tuple[float, float]" [arg-type]
Found 1 error in 1 file (checked 1 source file)
Expected outcome
No type errors.
Additional information
Versions:
python: 3.12
matplotlib: 3.10.3
basedpyright: 1.31.0
mypy: 1.17.0
Operating system
Debian (Linux Mint)
Matplotlib Version
3.10.3
Matplotlib Backend
qtagg
Python version
3.12
Jupyter version
No response
Installation
pip