Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a2e251a

Browse files
efiringtimhoffm
authored andcommitted
BUG: calculate colorbar boundaries correctly from values (#13114)
Closes #13098. The unusual case of a call to colorbar with specified values occurs in test_colorbar.py test_colorbar_closed_patch. The effect of the bug there was to disable the ticks; but evidently this was considered a feature, because the example image was made with the bug in place, hence with no ticks on the colorbars. Therefore I have modified the test code to explicitly disable the ticks, so the test images don't have to be changed.
1 parent 0c35833 commit a2e251a

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

lib/matplotlib/colorbar.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -791,7 +791,7 @@ def _process_values(self, b=None):
791791
self._values = np.array(self.values)
792792
if self.boundaries is None:
793793
b = np.zeros(len(self.values) + 1)
794-
b[1:-1] = 0.5 * (self._values[:-1] - self._values[1:])
794+
b[1:-1] = 0.5 * (self._values[:-1] + self._values[1:])
795795
b[0] = 2.0 * b[1] - b[2]
796796
b[-1] = 2.0 * b[-2] - b[-3]
797797
self._boundaries = b

lib/matplotlib/tests/test_colorbar.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -242,17 +242,21 @@ def test_colorbar_closed_patch():
242242
cmap = get_cmap("RdBu", lut=5)
243243

244244
im = ax1.pcolormesh(np.linspace(0, 10, 16).reshape((4, 4)), cmap=cmap)
245+
246+
# The use of a "values" kwarg here is unusual. It works only
247+
# because it is matched to the data range in the image and to
248+
# the number of colors in the LUT.
245249
values = np.linspace(0, 10, 5)
250+
cbar_kw = dict(cmap=cmap, orientation='horizontal', values=values,
251+
ticks=[])
246252

253+
# The wide line is to show that the closed path is being handled
254+
# correctly. See PR #4186.
247255
with rc_context({'axes.linewidth': 16}):
248-
plt.colorbar(im, cax=ax2, cmap=cmap, orientation='horizontal',
249-
extend='both', extendfrac=0.5, values=values)
250-
plt.colorbar(im, cax=ax3, cmap=cmap, orientation='horizontal',
251-
extend='both', values=values)
252-
plt.colorbar(im, cax=ax4, cmap=cmap, orientation='horizontal',
253-
extend='both', extendrect=True, values=values)
254-
plt.colorbar(im, cax=ax5, cmap=cmap, orientation='horizontal',
255-
extend='neither', values=values)
256+
plt.colorbar(im, cax=ax2, extend='both', extendfrac=0.5, **cbar_kw)
257+
plt.colorbar(im, cax=ax3, extend='both', **cbar_kw)
258+
plt.colorbar(im, cax=ax4, extend='both', extendrect=True, **cbar_kw)
259+
plt.colorbar(im, cax=ax5, extend='neither', **cbar_kw)
256260

257261

258262
def test_colorbar_ticks():

0 commit comments

Comments
 (0)