8000 colorbar: improve handling of manually-set vmin, vmax in contourf, co… · matplotlib/matplotlib@413581c · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 413581c

Browse files
committed
colorbar: improve handling of manually-set vmin, vmax in contourf, contour
Formerly, colorbar would not place ticks at contour levels outside the vmin-vmax range, and would not add line contour marks at such levels.
1 parent f5c85d8 commit 413581c

File tree

2 files changed

+30
-21
lines changed

2 files changed

+30
-21
lines changed

lib/matplotlib/colorbar.py

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -470,11 +470,18 @@ def _add_solids(self, X, Y, C):
470470
def add_lines(self, levels, colors, linewidths):
471471
'''
472472
Draw lines on the colorbar.
473+
474+
*colors* and *linewidths* must be scalars or
475+
sequences the same length as *levels*.
473476
'''
474-
N = len(levels)
475-
dummy, y = self._locate(levels)
476-
if len(y) != N:
477-
raise ValueError("levels are outside colorbar range")
477+
y = self._locate(levels)
478+
igood = (y < 1.001) & (y > -0.001)
479+
y = y[igood]
480+
if cbook.iterable(colors):
481+
colors = np.asarray(colors)[igood]
482+
if cbook.iterable(linewidths):
483+
linewidths = np.asarray(linewidths)[igood]
484+
N = len(y)
478485
x = np.array([0.0, 1.0])
479486
X, Y = np.meshgrid(x,y)
480487
if self.orientation == 'vertical':
@@ -528,7 +535,10 @@ def _ticker(self):
528535
locator.axis.get_minpos = lambda : intv[0]
529536
formatter.axis.get_minpos = lambda : intv[0]
530537
b = np.array(locator())
531-
b, ticks = self._locate(b)
538+
ticks = self._locate(b)
539+
inrange = (ticks > -0.001) & (ticks < 1.001)
540+
ticks = ticks[inrange]
541+
b = b[inrange]
532542
formatter.set_locs(b)
533543
ticklabels = [formatter(t, i) for i, t in enumerate(b)]
534544
offset_string = formatter.get_offset()
@@ -746,37 +756,36 @@ def _mesh(self):
746756

747757
def _locate(self, x):
748758
'''
749-
Given a possible set of color data values, return the ones
750-
within range, together with their corresponding colorbar
751-
data coordinates.
759+
Given a set of color data values, return their
760+
corresponding colorbar data coordinates.
752761
'''
753762
if isinstance(self.norm, (colors.NoNorm, colors.BoundaryNorm)):
754763
b = self._boundaries
755764
xn = x
756-
xout = x
757765
else:
758766
# Do calculations using normalized coordinates so
759767
# as to make the interpolation more accurate.
760768
b = self.norm(self._boundaries, clip=False).filled()
761-
# We do our own clipping so that we can allow a tiny
762-
# bit of slop in the end point ticks to allow for
763-
# floating point errors.
764769
xn = self.norm(x, clip=False).filled()
765-
in_cond = (xn > -0.001) & (xn < 1.001)
766-
xn = np.compress(in_cond, xn)
767-
xout = np.compress(in_cond, x)
768-
# The rest is linear interpolation with clipping.
770+
# The rest is linear interpolation with extrapolation at ends.
769771
y = self._y
770772
N = len(b)
771-
ii = np.minimum(np.searchsorted(b, xn), N-1)
772-
i0 = np.maximum(ii - 1, 0)
773+
ii = np.searchsorted(b, xn)
774+
i0 = ii - 1
775+
itop = (ii == N)
776+
ibot = (ii == 0)
777+
i0[itop] -= 1
778+
ii[itop] -= 1
779+
i0[ibot] += 1
780+
ii[ibot] += 1
781+
773782
#db = b[ii] - b[i0]
774783
db = np.take(b, ii) - np.take(b, i0)
775-
db = np.where(i0==ii, 1.0, db)
776784
#dy = y[ii] - y[i0]
777785
dy = np.take(y, ii) - np.take(y, i0)
778786
z = np.take(y, i0) + (xn-np.take(b,i0))*dy/db
779-
return xout, z
787+
788+
return z
780789

781790
def set_alpha(self, alpha):
782791
self.alpha = alpha

lib/matplotlib/contour.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1091,7 +1091,7 @@ def _process_colors(self):
10911091
line contours, when added to filled contours, take on
10921092
colors that are consistent with those of the filled regions;
10931093
for example, a contour line on the boundary between two
1094-
regions will have a color intermediate between the those
1094+
regions will have a color intermediate between those
10951095
of the regions.
10961096
10971097
"""

0 commit comments

Comments
 (0)
0