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

Skip to content

[WIP] Attempted fix to colorbar bug when yscaled len = 1 as suggested in previous PR #24656

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

quinnah
Copy link

@quinnah quinnah commented Dec 7, 2022

PR Summary

Opening a draft PR for issue #23817, building off the work of @Andes0113 in PR #23984 and attempting to address the issues mentioned there.

When I run the test, I'm able to fix the underlying issue by using the same yscaled value for
automin and automax. However, later on in the test, I run into a warning on the fig.colorbar(cs, ax):

$ pytest lib/matplotlib/tests/test_colorbar.py -k 'test_contour_uniformfield_colorbar'

UserWarning: Attempting to set identical low and high ylims makes transformation singular; automatically expanding.

When I look at the stack trace, I see the following (I omitted decorator wrapping frames):

lib/matplotlib/figure.py:1277: in colorbar
    cb = cbar.Colorbar(cax, mappable, **cb_kw)
lib/matplotlib/colorbar.py:420: in __init__
    self._draw_all()
lib/matplotlib/colorbar.py:566: in _draw_all
    self.ax.set_ylim(lower, upper)

This makes me think that the warning has to do with the axis set on the plot. Online, posts seem to suggest that this error comes up when generating plots with no spread in the y-axis. That zero-width
spread on the y-axis seems critical to the test though, so I'm not sure how to proceed.

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

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.

@jklymak
Copy link
Member

jklymak commented Dec 7, 2022

Please indicate the issue being fixed, and include code snippet that you are testing with.

@quinnah
Copy link
Author

quinnah commented Dec 7, 2022

The issue being addressed is handling automin and automax when yscaled len = 1, in the def _proportional_y function in test/colorbar.py.

 if self._extend_lower() or self._extend_upper():
            automin = yscaled[0]
            automax = yscaled[0]
            if len(yscaled) > 1:
                automin = yscaled[1] - yscaled[0]
                automax = yscaled[-1] - yscaled[-2]

The (failing) test generating the warning is:

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

Looking at the stack trace, the warning seems to come from self.ax.set_ylim(lower, upper) where lower == upper. Looking for other instances of this particular warning (such as here), it appears to come from setting the min and max y values to be the same. This makes me question what the expected behavior from the test should be.

@TianzeMYou
Copy link

TianzeMYou commented Dec 12, 2022

The original code for reproduction of this issue 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.

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

@melissawm
Copy link
Member

Hi @quinnah - is this ready for review? Do you need help with moving this forward? Cheers!

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