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

Skip to content

Suppresses failed test case by excepting TypeError from _initialize_x_y #24698

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

Closed

Conversation

TianzeMYou
Copy link

@TianzeMYou TianzeMYou commented Dec 12, 2022

PR Summary

The original code for reproduction of this issue #23817 is

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
cs = ax.contour([[1, 1], [1, 1]])
fig.colorbar(cs, ax=ax)
plt.show()

However, this seems to yield the following TypeError
TypeError: Input z must be 2D, not 1D

The same TypeError also occurs if we change

cs = ax.contour([[1, 1], [1, 1]])

to

cs = ax.contour([[1, 2], [3, 4]])

This TypeError is resulted from lib/matplotlib/contour.py, line 1526, in _initialize_x_y, which makes me think that rather than a colorbar problem, the root of this issue lies in how contour works. What is the expected behavior of the original code for reproduction? If we aren't supposed to call Fontour on input like above in the first place, how is Colorbar supposed to behave and why does the the API warning even matter?

I was able to suppress the failed test case with the following change to @quinnah's test case from #24656

def test_contour_uniformfield_colorbar():
    # Smoke test for issue
    fig, ax = plt.subplots()
    with pytest.warns(Warning) as record:
        cs = ax.contour([[2, 2], [2, 2]])
    assert len(record) == 1
    try:
        fig.colorbar(cs, ax=ax)
    except:
        pass

PR Checklist

Documentation and Tests

  • Has pytest style unit tests (and pytest passes)
  • Documentation is sphinx and numpydoc compliant (the docs should build without error).
  • New plotting related features are documented with examples.

Release Notes

  • New features are marked with a .. versionadded:: directive in the docstring and documented in doc/users/next_whats_new/
  • API changes are marked with a .. versionchanged:: directive in the docstring and documented in doc/api/next_api_changes/
  • Release notes conform with instructions in next_whats_new/README.rst or next_api_changes/README.rst

@TianzeMYou TianzeMYou marked this pull request as draft December 12, 2022 02:47
Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Thank you for opening your first PR into Matplotlib!

If you have not heard from us in a while, please feel free to ping @matplotlib/developers or anyone who has commented on the PR. 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

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

y = y / (self._boundaries[self._inside][-1] -
self._boundaries[self._inside][0])

# original
Copy link
Member

Choose a reason for hiding this comment

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

Please do not keep the original code around. Because the code is in git and there are a number of UIs (inculding this website) for viewing the diffs it is much clearer to simply remove the old code and replace it with the new code. If we kept the full history of Matplotib in-line like this it would very quickly become impossible to read!

cs = ax.contour([[2, 2], [2, 2]])
assert len(record) == 1
try:
fig.colorbar(cs, ax=ax)
Copy link
Member

Choose a reason for hiding this comment

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

Why is this in a try-except?

# self._boundaries[self._inside][0])

# change
if (self._boundaries[self._inside][-1]
Copy link
Member

Choose a reason for hiding this comment

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

does this need an else branch?

@melissawm
Copy link
Member

👋🏻 Hi @TianzeMYou - gentle ping that you have a pending review on this PR, are you still interested in working on it? If you need help, please let us know. Cheers!

@timhoffm
Copy link
Member

timhoffm commented Feb 2, 2025

The issue has been solved through another fix.

@timhoffm timhoffm closed this Feb 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Waiting for author
Development

Successfully merging this pull request may close these issues.

4 participants