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

[Bug]: Error showing colorbar when contouring a uniform field · Issue #23817 · matplotlib/matplotlib · GitHub

Repository navigation

[Bug]: Error showing colorbar when contouring a uniform field #23817

Closed

Description

Bug summary

If you contour a uniform field i.e. ax.contour(z=[[1, 1], [1,1]])) it works fine, although the output is not very interesting. If you add a colorbar to this, you receive an error. Without the colorbar call there is no error.

Code for reproduction

import matplotlib.pyplot as plt

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

Actual outcome

bug.py:18: UserWarning: No contour levels were found within the data range.
cs = ax.contour([[1, 1], [1, 1]])
/home/iant/github/matplotlib/lib/matplotlib/colorbar.py:1224: RuntimeWarning: invalid value encountered in divide
y = y / (self._boundaries[self._inside][-1] -
Traceback (most recent call last):
File "bug.py", line 19, in
fig.colorbar(cs, ax=ax)
File "/home/iant/github/matplotlib/lib/matplotlib/figure.py", line 1276, in colorbar
cb = cbar.Colorbar(cax, mappable, **cb_kw)
File "/home/iant/github/matplotlib/lib/matplotlib/_api/deprecation.py", line 384, in wrapper
return func(*inner_args, **inner_kwargs)
File "/home/iant/github/matplotlib/lib/matplotlib/colorbar.py", line 396, in init
self._draw_all()
File "/home/iant/github/matplotlib/lib/matplotlib/colorbar.py", line 535, in _draw_all
X, Y = self._mesh()
File "/home/iant/github/matplotlib/lib/matplotlib/colorbar.py", line 1110, in _mesh
y, _ = self._proportional_y()
File "/home/iant/github/matplotlib/lib/matplotlib/colorbar.py", line 1246, in _proportional_y
automin = yscaled[1] - yscaled[0]
IndexError: index 1 is out of bounds for axis 0 with size 1

Expected outcome

Colorbar should be displayed as normal.

Additional information

Essentially the colorbar data range is zero, so when trying to determine the y-position of a particular contour level there is a divide by zero.

This has probably been a problem for years. It is a silly test case as who wants to contour a uniform field, but we should handle it more gracefully.

Operating system

N/A

Matplotlib Version

3.6.0.dev3132+gf8cf0ee5e7

Matplotlib Backend

N/A

Python version

3.8.10

Jupyter version

N/A

Installation

from source (.tar.gz)

Activity

  1. oscargus commented on Sep 12, 2022

    Member

    Marking this a good first issue. It is probably enough to check the length of yscaled here and if it is one, just set it to the value.

    # make the lower and upper extend lengths proportional to the lengths
    # of the first and last boundary spacing (if extendfrac='auto'):
    automin = yscaled[1] - yscaled[0]
    automax = yscaled[-1] - yscaled[-2]
    extendlength = [0, 0]
    if self._extend_lower() or self._extend_upper():
    extendlength = self._get_extension_lengths(
    self.extendfrac, automin, automax, default=0.05)
    return y, extendlength

    Also, the automin and automax computation may very well be moved inside the if-clause.

    (Subject to checking what actually happens, but at least it shouldn't error out there anymore...)

    Tests should include all variants of extend (as moving it inside the if-clause should solve the case here.)

  2. added
    🌱 Good first issueOpen a pull request against these issues if there are no active ones!
    Difficulty: Easyhttps://matplotlib.org/devdocs/devel/contribute.html#good-first-issues
    on Sep 12, 2022
  3. rebeccajhampton commented on Sep 14, 2022

    I would like to take a go at this as a first time contributor. Can I take this issue?

  4. jklymak commented on Sep 14, 2022

    Member

    @rebeccajhampton feel free, though note we do not reserve issues. Also feel free to join the incubator on our gitter channel

  5. joshfine2003 commented on Sep 14, 2022

    I'm going to try and tackle this one as well

  6. jklymak commented on Sep 14, 2022

    Member

    We do t reserve issues, but also it's nice not to duplicate effort if possible. Maybe wait a few days and see if @rebeccajhampton puts in a PR and then help?

  7. Andes0113 commented on Sep 22, 2022

    Opened a pull request on this issue, check it out if you have the chance: #23984

  8. quinnah commented on Nov 26, 2022

    Seeing as the linked PR failed review, is this issue open?

  9. Andes0113 commented on Nov 26, 2022

    Yep, feel free to pick it up if you wish. It ended up being a lot more work of tracking down errors than anticipated, so I shelved it for the time being and never got back around to working on it again.

  10. greglucas commented on Mar 9, 2023

    Contributor

    This doesn't issue an error on 3.7.1 anymore. I didn't bisect it, but possibly from this PR: #24912

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Difficulty: Easyhttps://matplotlib.org/devdocs/devel/contribute.html#good-first-issuestopic: color/colorbar🌱 Good first issueOpen a pull request against these issues if there are no active ones!

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL