Title: [Bug]: Type hints for xy and xycoords in annotate are too strict · Issue #30365 · matplotlib/matplotlib · GitHub
Open Graph Title: [Bug]: Type hints for xy and xycoords in annotate are too strict · Issue #30365 · matplotlib/matplotlib
X Title: [Bug]: Type hints for xy and xycoords in annotate are too strict · Issue #30365 · matplotlib/matplotlib
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...
Open Graph 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...
X 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 plo...
Opengraph URL: https://github.com/matplotlib/matplotlib/issues/30365
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[Bug]: Type hints for xy and xycoords in annotate are too strict","articleBody":"### Bug summary\n\nRight 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.\n\nThere 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.\n\n### Code for reproduction\n\n```Python\nfrom typing import cast\n\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd\n\nidx = pd.date_range(start='01/01/2014', end='03/01/2014', freq='1h')\nn = len(idx)\nrng = np.random.default_rng()\ndata = rng.normal(size=n)\ndf = pd.DataFrame({\"data\": data}, index=idx)\n\ndaily = df.resample(\"D\")\nmaxs = daily.max()\nmins = daily.min()\n\nax = maxs.plot()\nmins.plot(ax=ax)\nfor t, x in maxs[\"data\"].items():\n t = cast(pd.Timestamp, t)\n x = cast(float, x)\n ax.annotate(str(x), (t, x))\n\nplt.show()\n```\n\n### Actual outcome\n\n$ basedpyright dummy1.py \n/home/andrea/Code/playground/dummy1.py\n /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\"\n \"Timestamp\" is not assignable to \"float\" (reportArgumentType)\n1 error, 0 warnings, 0 notes\n\n$ mypy dummy1.py\ndummy1.py:26: error: Argument 2 to \"annotate\" of \"Axes\" has incompatible type \"tuple[Timestamp, float]\"; expected \"tuple[float, float]\" [arg-type]\nFound 1 error in 1 file (checked 1 source file)\n\n### Expected outcome\n\nNo type errors.\n\n### Additional information\n\nVersions:\npython: 3.12\nmatplotlib: 3.10.3\nbasedpyright: 1.31.0\nmypy: 1.17.0\n\n### Operating system\n\nDebian (Linux Mint)\n\n### Matplotlib Version\n\n3.10.3\n\n### Matplotlib Backend\n\nqtagg\n\n### Python version\n\n3.12\n\n### Jupyter version\n\n_No response_\n\n### Installation\n\npip","author":{"url":"https://github.com/Jeitan","@type":"Person","name":"Jeitan"},"datePublished":"2025-07-28T18:28:00.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":15},"url":"https://github.com/30365/matplotlib/issues/30365"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:e7d0057c-3073-bfa3-2c20-7f8ccc19638c |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9BF4:22A568:27FB83:34E07E:6A526E4E |
| html-safe-nonce | 4c9b9719551c2c3ebce3d1d5f9be99ac374cf4b318e3b85e59b0d11c2bac23f4 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5QkY0OjIyQTU2ODoyN0ZCODM6MzRFMDdFOjZBNTI2RTRFIiwidmlzaXRvcl9pZCI6IjM4Mzk4MTU5MDgxODYwMjU1NTAiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 95b177d852bf134eee1b867a966a462c75f141b23d05a7f6ac54ed9b7d5b8107 |
| hovercard-subject-tag | issue:3270941390 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/matplotlib/matplotlib/30365/issue_layout |
| twitter:image | https://opengraph.githubassets.com/09e61e6f9575fa60468b0a8355d31ddd5ea48f3d77c995b3c9581d8df9d42fa4/matplotlib/matplotlib/issues/30365 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/09e61e6f9575fa60468b0a8355d31ddd5ea48f3d77c995b3c9581d8df9d42fa4/matplotlib/matplotlib/issues/30365 |
| og:image:alt | 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... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Jeitan |
| hostname | github.com |
| expected-hostname | github.com |
| None | b9a586c06a05a7a86fc7e3f4dbd03e42f6869085879aa184aa6369456dbd50fb |
| turbo-cache-control | no-preview |
| go-import | github.com/matplotlib/matplotlib git https://github.com/matplotlib/matplotlib.git |
| octolytics-dimension-user_id | 215947 |
| octolytics-dimension-user_login | matplotlib |
| octolytics-dimension-repository_id | 1385122 |
| octolytics-dimension-repository_nwo | matplotlib/matplotlib |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 1385122 |
| octolytics-dimension-repository_network_root_nwo | matplotlib/matplotlib |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 07a982c1d40157c619b364352b704c3ce66bb332 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width