Skip to content

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

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
ianthomas23 opened this issue Sep 7, 2022 · 9 comments
Closed

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

ianthomas23 opened this issue Sep 7, 2022 · 9 comments
Labels
Difficulty: Easy https://matplotlib.org/devdocs/devel/contribute.html#good-first-issues Good first issue Open a pull request against these issues if there are no active ones! topic: color/colorbar

Comments

@ianthomas23
Copy link
Member

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)

@oscargus
Copy link
Member

oscargus commented Sep 12, 2022

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.)

@oscargus oscargus added Good first issue Open a pull request against these issues if there are no active ones! topic: color/colorbar Difficulty: Easy https://matplotlib.org/devdocs/devel/contribute.html#good-first-issues labels Sep 12, 2022
@rebeccajhampton
Copy link

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

@jklymak
Copy link
Member

jklymak commented Sep 14, 2022

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

@joshfine2003
Copy link

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

@jklymak
Copy link
Member

jklymak commented Sep 14, 2022

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?

@Andes0113
Copy link

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

@oscargus oscargus linked a pull request Sep 29, 2022 that will close this issue
2 tasks
@quinnah
Copy link

quinnah commented Nov 26, 2022

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

@Andes0113
Copy link

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.

@greglucas
Copy link
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
Labels
Difficulty: Easy https://matplotlib.org/devdocs/devel/contribute.html#good-first-issues Good first issue Open a pull request against these issues if there are no active ones! topic: color/colorbar
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants