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.
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.
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?
Reacted by Noa Tamir, hannah, rebeccajhampton and Epsilonator
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.
Activity
oscargus commented on 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.
matplotlib/lib/matplotlib/colorbar.py
Lines 1244 to 1252 in 9f17f3f
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.)
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?
jklymak commented on 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 commented on Sep 14, 2022
I'm going to try and tackle this one as well
jklymak commented on 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 commented on Sep 22, 2022
Opened a pull request on this issue, check it out if you have the chance: #23984
quinnah commented on Nov 26, 2022
Seeing as the linked PR failed review, is this issue open?
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.
greglucas commented on Mar 9, 2023
This doesn't issue an error on 3.7.1 anymore. I didn't bisect it, but possibly from this PR: #24912