FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[WIP] Attempted fix to colorbar bug when yscaled len = 1 as suggested in previous PR by quinnah · Pull Request #24656 · matplotlib/matplotlib · GitHub

Repository navigation

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

Draft
quinnah wants to merge 1 commit into
matplotlib:mainfrom
quinnah:colorbar-contour-fix
Draft

quinnah wants to merge 1 commit into
matplotlib:mainfrom
quinnah:colorbar-contour-fix

Conversation

quinnah commented Dec 7, 2022

Copy link
Copy Markdown

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

github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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 commented Dec 7, 2022

Copy link
Copy Markdown
Member

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

quinnah commented Dec 7, 2022 •
edited
Loading

Copy link
Copy Markdown
Author

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 commented Dec 12, 2022 •
edited by QuLogic
Loading

Copy link
Copy Markdown

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

TianzeMYou added a commit to TianzeMYou/matplotlib that referenced this pull request Dec 12, 2022

Copy link
Copy Markdown
Member

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

Copy link
Copy Markdown

Since this Pull Request has not been updated in 60 days, it has been marked "inactive." This does not mean that it will be closed, though it may be moved to a "Draft" state. This helps maintainers prioritize their reviewing efforts. You can pick the PR back up anytime - please ping us if you need a review or guidance to move the PR forward! If you do not plan on continuing the work, please let us know so that we can either find someone to take the PR over, or close it.

github-actions Bot added the status: inactive Marked by the “Stale” Github Action label Aug 24, 2026

This branch has not been deployed

No deployments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: inactive Marked by the “Stale” Github Action

Projects

Status: Waiting for author

Development

Successfully merging this pull request may close these issues.

4 participants


Back | FazBrowse Home | New Git URL