-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
[Bug]: Horizontal colorbars drawn incorrectly with hatches #23456
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
Did this work before and we broke it, or did it never work? There were some changes to colorbars, but I'm not sure why they would cause this to happen. Thanks. |
I'm not sure but I can try running my code using older versions of matplotlib. What version do you think might be the culprit? |
Try 3.4.2. But if you don't know if this ever worked it probably never did. My guess would be there is a typo in the code that makes the hatch. Maybe east to fix if you grep hatch in the colorbar.py |
The bug still happens in v3.4.2. I did find a solution that fixes the issue without messing up vertical colorbars with hatches. I works in both 3.5.2 and 3.4.2 def _add_solids_patches(self, X, Y, C, mappable):
hatches = mappable.hatches * len(C) # Have enough hatches.
patches = []
for i in range(len(X) - 1):
xy = np.array([[X[i, 0], Y[i, 0]],
[X[i, 1], Y[i, 0]],
[X[i + 1, 1], Y[i + 1, 0]],
[X[i + 1, 0], Y[i + 1, 1]]]) To this def _add_solids_patches(self, X, Y, C, mappable):
hatches = mappable.hatches * len(C) # Have enough hatches.
patches = []
for i in range(len(X) - 1):
xy = np.array([[X[i, 0], Y[i, 1]], # this is the only line that needs a change
[X[i, 1], Y[i, 0]],
[X[i + 1, 1], Y[i + 1, 0]],
[X[i + 1, 0], Y[i + 1, 1]]]) |
Yep, that looks like an indexing bug... If you have a patch, feel free to try a pull request. Maybe also add a test to test_colorbar.py? It would require an image comparison. https://matplotlib.org/stable/devel/index.html for more guidance, also feel free to ask at https://gitter.im/matplotlib/matplotlib |
The problem is related to |
So, matplotlib/lib/matplotlib/colorbar.py Line 610 in 9b1fcf6
and then picks one at a time starting from 0: matplotlib/lib/matplotlib/colorbar.py Line 619 in 9b1fcf6
The extend patches picks the first and last hatch pattern of the original list: matplotlib/lib/matplotlib/colorbar.py Line 685 in 9b1fcf6
matplotlib/lib/matplotlib/colorbar.py Line 706 in 9b1fcf6
|
Bug summary
When specifying hatches for
contourf,
the colorbar is drawn correctly for vertical colorbars but not for horizontal ones. The Hatches and colorfill only fill the lower diagonal of the colorbar boxes.Code for reproduction
Actual outcome
Expected outcome
Additional information
The bug occurs when the colorbar is horizontal and the
contourf
call uses hatches. Changing the kwargsextend
,cmap
,colors
,alpha
, etc when callingcontourf
does not resolve the bug.The bug does not occur when the colorbar is vertical with hatches nor when the colorbar is horizontal and no hatches are used.
Potential Solution:
In colorbar.py, change line 637 to the following:
xy = np.array([[X[i, 0], Y[i, 1]],
This doesn't cause issues when hatches are not used. It also doesn't cause problems with vertical colorbars.
Operating system
macOS Big Sur Version 11.3.1 with Intel Silicon
Matplotlib Version
3.5.2
Matplotlib Backend
module://backend_interagg
Python version
3.10.5
Jupyter version
No response
Installation
conda
The text was updated successfully, but these errors were encountered: