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
importmatplotlib.pyplotaspltimportnumpyasnpimportmatplotlib.pyplotaspltimportnumpyasnp# invent some numbers, turning the x and y arrays into simple# 2d arrays, which make combining them together easier.x=np.linspace(-3, 5, 150).reshape(1, -1)
y=np.linspace(-3, 5, 120).reshape(-1, 1)
z=np.cos(x) +np.sin(y)
# we no longer need x and y to be 2 dimensional, so flatten them.x, y=x.flatten(), y.flatten()
fig1, ax1=plt.subplots()
cs=ax1.contourf(x, y, z, hatches=['-', '/', '\\', '//'],
cmap='gray', extend='both', alpha=0.5)
fig1.colorbar(cs, orientation='horizontal')
plt.show()
Actual outcome
Expected outcome
Additional information
The bug occurs when the colorbar is horizontal and the contourf call uses hatches. Changing the kwargs extend, cmap, colors, alpha, etc when calling contourf 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.
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.
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
In _add_solids_patches, change the indices for creating xy from
def_add_solids_patches(self, X, Y, C, mappable):
hatches=mappable.hatches*len(C) # Have enough hatches.patches= []
foriinrange(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]]])
I was about to implement this, but I found another issue as well. A slightly different setting gave this (after the patch above was introduced):
Clearly the colors and hatches are off by one.
That seems to be a problem for vertical as well (and maybe easier to see there):
The same thing can be seen in the original picture although not as obvious there. Rather, there are eight different levels in both the contour and the colorbar, but the left extended level in the colorbar is not matching the darkest in the contour. However, of the three lightest/right-most three levels in the contour, only two of them are in the colorbar.
Not sure if it is worthwhile doing a patch to get the rectangle correct as it probably doesn't make sense to add a test image since the colorbar in itself doesn't match the contour-levels.
Activity
jklymak commented on Jul 20, 2022
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.
hCraker commented on Jul 21, 2022
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?
jklymak commented on Jul 21, 2022
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
hCraker commented on Jul 21, 2022
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
In _add_solids_patches, change the indices for creating xy from
To this
jklymak commented on Jul 21, 2022
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
oscargus commented on Aug 20, 2022
I was about to implement this, but I found another issue as well. A slightly different setting gave this (after the patch above was introduced):
Clearly the colors and hatches are off by one.
That seems to be a problem for vertical as well (and maybe easier to see there):

The same thing can be seen in the original picture although not as obvious there. Rather, there are eight different levels in both the contour and the colorbar, but the left extended level in the colorbar is not matching the darkest in the contour. However, of the three lightest/right-most three levels in the contour, only two of them are in the colorbar.
Not sure if it is worthwhile doing a patch to get the rectangle correct as it probably doesn't make sense to add a test image since the colorbar in itself doesn't match the contour-levels.
oscargus commented on Aug 20, 2022
The problem is related to extend. Without it, it is all good.
oscargus commented on Aug 20, 2022
So, _add_solids_patches duplicates the hatches:
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