-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
[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
Comments
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.) |
I would like to take a go at this as a first time contributor. Can I take this issue? |
@rebeccajhampton feel free, though note we do not reserve issues. Also feel free to join the incubator on our gitter channel |
I'm going to try and tackle this one as well |
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? |
Opened a pull request on this issue, check it out if you have the chance: #23984 |
Seeing as the linked PR failed review, is this issue open? |
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. |
This doesn't issue an error on 3.7.1 anymore. I didn't bisect it, but possibly from this PR: #24912 |
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
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)
The text was updated successfully, but these errors were encountered: