X Tutup
Skip to content

Fix clabel manual argument not accepting unit-typed coordinates#31278

Merged
story645 merged 4 commits intomatplotlib:mainfrom
aman-coder03:fix/clabel-manual-units
Mar 11, 2026
Merged

Fix clabel manual argument not accepting unit-typed coordinates#31278
story645 merged 4 commits intomatplotlib:mainfrom
aman-coder03:fix/clabel-manual-units

Conversation

@aman-coder03
Copy link
Copy Markdown
Contributor

PR summary

when unit typed coordinates(e.g datetime) are passed to the manual argument of clabel...they were passed directly to transform.transform() without unit conversion, causing a TypeError because the transform expects float64 but received Python objects

fixed by calling self.axes.convert_xunits(x) and self.axes.convert_yunits(y) in add_label_near() before applying the transform. This is the same pattern already used elsewhere in _base.py and is a no-op when nounit converter is registered, so non-unit axes are unaffected

AI Disclosure

PR checklist

@github-actions
Copy link
Copy Markdown

Thank you for opening your first PR into Matplotlib!

If you have not heard from us in a week or so, please leave a new comment below and that should bring it to our attention. Most of our reviewers are volunteers and sometimes things fall through the cracks.

You can also join us on gitter for real-time discussion.

For details on testing, writing docs, and our review process, please see the developer guide.
Please let us know if (and how) you use AI, it will help us give you better feedback on your PR.

We strive to be a welcoming and open project. Please follow our Code of Conduct.

@rcomer
Copy link
Copy Markdown
Member

rcomer commented Mar 11, 2026

Hi @aman-coder03 thank you for your interest in contributing to Matplotlib! Please can you run the example from the issue with your branch, and post the image that it now produces.

@aman-coder03
Copy link
Copy Markdown
Contributor Author

sure! here is the output produced by the example from the issue with my fix applied
image

@aman-coder03
Copy link
Copy Markdown
Contributor Author

failing tests are unrelated to this fix test_webagg is a known flaky timeout issue and test_upsample_interpolation_stage is a subprocess resource warning... all other 9921 tests pass

@rcomer
Copy link
Copy Markdown
Member

rcomer commented Mar 11, 2026

Thanks for posting the image. I agree that looks correct. Please can you modify this test so it covers the case you are fixing.

@pytest.mark.xfail(reason="Test for clabel not written yet")
@mpl.style.context("default")
def test_clabel(self):
fig, ax = plt.subplots()
ax.clabel(...)

@aman-coder03
Copy link
Copy Markdown
Contributor Author

@rcomer i have added the test now!

@rcomer
Copy link
Copy Markdown
Member

rcomer commented Mar 11, 2026

Thanks for adding the test. Is there any way it could be simplified, but still fail without your change?

@aman-coder03
Copy link
Copy Markdown
Contributor Author

yes @rcomer working on simplifying it, will push a commit shortly

@melissawm melissawm moved this to Needs review in First Time Contributors Mar 11, 2026
Copy link
Copy Markdown
Member

@rcomer rcomer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @aman-coder03. This seems right to me.

fig, ax = plt.subplots()
ax.clabel(...)
CS = ax.contour(X, Y, Z)
ax.clabel(CS, manual=[(x[0], dates[0])])
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you test that the label is set to the expected value?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure i can add an assertion on the label value, what would be the best way to check it, assert on the text string or the position?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

@story645 story645 modified the milestones: v3.11.0, v3.10.9 Mar 11, 2026
@story645 story645 merged commit a01f57d into matplotlib:main Mar 11, 2026
32 of 36 checks passed
@github-project-automation github-project-automation bot moved this from Needs review to Merged in First Time Contributors Mar 11, 2026
meeseeksmachine pushed a commit to meeseeksmachine/matplotlib that referenced this pull request Mar 11, 2026
andreas16700 added a commit to andreas16700/matplotlib that referenced this pull request Mar 16, 2026
andreas16700 added a commit to andreas16700/matplotlib that referenced this pull request Mar 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Development

Successfully merging this pull request may close these issues.

[Bug]: clabel manual argument does not accept units

4 participants

X Tutup