Skip to content

Commit 9fea430

Browse files
committed
Merge pull request #2858 from efiring/colorbar_one_value
BUG: colorbar autoscaling now ensures a finite range of values
2 parents b5a6c49 + e9f9e6c commit 9fea430

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

lib/matplotlib/colorbar.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -551,8 +551,8 @@ def add_lines(self, levels, colors, linewidths, erase=True):
551551

552552
def _ticker(self):
553553
'''
554-
Return two sequences: ticks (colorbar data locations)
555-
and ticklabels (strings).
554+
Return the sequence of ticks (colorbar data locations),
555+
ticklabels (strings), and the corresponding offset string.
556556
'''
557557
locator = self.locator
558558
formatter = self.formatter
@@ -653,6 +653,9 @@ def _process_values(self, b=None):
653653
self._values = v
654654
return
655655
else:
656+
self.norm.vmin, self.norm.vmax = mtrans.nonsingular(self.norm.vmin,
657+
self.norm.vmax,
658+
expander=0.1)
656659
if not self.norm.scaled():
657660
self.norm.vmin = 0
658661
self.norm.vmax = 1
@@ -817,24 +820,21 @@ def _locate(self, x):
817820
# as to make the interpolation more accurate.
818821
b = self.norm(self._boundaries, clip=False).filled()
819822
xn = self.norm(x, clip=False).filled()
823+
820824
# The rest is linear interpolation with extrapolation at ends.
821-
y = self._y
822-
N = len(b)
823825
ii = np.searchsorted(b, xn)
824826
i0 = ii - 1
825-
itop = (ii == N)
827+
itop = (ii == len(b))
826828
ibot = (ii == 0)
827829
i0[itop] -= 1
828830
ii[itop] -= 1
829831
i0[ibot] += 1
830832
ii[ibot] += 1
831833

832-
#db = b[ii] - b[i0]
833834
db = np.take(b, ii) - np.take(b, i0)
834-
#dy = y[ii] - y[i0]
835+
y = self._y
835836
dy = np.take(y, ii) - np.take(y, i0)
836837
z = np.take(y, i0) + (xn - np.take(b, i0)) * dy / db
837-
838838
return z
839839

840840
def set_alpha(self, alpha):

lib/matplotlib/tests/test_colorbar.py

+16
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,22 @@ def test_gridspec_make_colorbar():
195195
plt.subplots_adjust(top=0.95, right=0.95, bottom=0.2, hspace=0.25)
196196

197197

198+
@image_comparison(baseline_images=['colorbar_single_scatter'],
199+
extensions=['png'], remove_text=True,
200+
savefig_kwarg={'dpi': 40})
201+
def test_colorbar_single_scatter():
202+
# Issue #2642: if a path collection has only one entry,
203+
# the norm scaling within the colorbar must ensure a
204+
# finite range, otherwise a zero denominator will occur in _locate.
205+
plt.figure()
206+
x = np.arange(4)
207+
y = x.copy()
208+
z = np.ma.masked_greater(np.arange(50, 54), 50)
209+
cmap = plt.get_cmap('jet', 16)
210+
cs = plt.scatter(x, y, z, c=z, cmap=cmap)
211+
plt.colorbar(cs)
212+
213+
198214
if __name__ == '__main__':
199215
import nose
200216
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)

0 commit comments

Comments
 (0)