diff --git a/lib/matplotlib/colorbar.py b/lib/matplotlib/colorbar.py
index 0dbe6bdc6109..65166bea0974 100644
--- a/lib/matplotlib/colorbar.py
+++ b/lib/matplotlib/colorbar.py
@@ -705,13 +705,14 @@ def add_lines(self, levels, colors, linewidths, erase=True):
removing any previously added lines.
'''
y = self._locate(levels)
- igood = (y < 1.001) & (y > -0.001)
+ rtol = (self._y[-1] - self._y[0]) * 1e-10
+ igood = (y < self._y[-1] + rtol) & (y > self._y[0] - rtol)
y = y[igood]
if np.iterable(colors):
colors = np.asarray(colors)[igood]
if np.iterable(linewidths):
linewidths = np.asarray(linewidths)[igood]
- X, Y = np.meshgrid([0, 1], y)
+ X, Y = np.meshgrid([self._y[0], self._y[-1]], y)
if self.orientation == 'vertical':
xy = np.stack([X, Y], axis=-1)
else:
diff --git a/lib/matplotlib/tests/baseline_images/test_axes/contour_colorbar.pdf b/lib/matplotlib/tests/baseline_images/test_axes/contour_colorbar.pdf
index f4090c83aad3..8c1f477a66b1 100644
Binary files a/lib/matplotlib/tests/baseline_images/test_axes/contour_colorbar.pdf and b/lib/matplotlib/tests/baseline_images/test_axes/contour_colorbar.pdf differ
diff --git a/lib/matplotlib/tests/baseline_images/test_axes/contour_colorbar.png b/lib/matplotlib/tests/baseline_images/test_axes/contour_colorbar.png
index abca7ea4a6bb..002557764760 100644
Binary files a/lib/matplotlib/tests/baseline_images/test_axes/contour_colorbar.png and b/lib/matplotlib/tests/baseline_images/test_axes/contour_colorbar.png differ
diff --git a/lib/matplotlib/tests/baseline_images/test_axes/contour_colorbar.svg b/lib/matplotlib/tests/baseline_images/test_axes/contour_colorbar.svg
index b60e679f6b3a..c5b68c60ecd0 100644
--- a/lib/matplotlib/tests/baseline_images/test_axes/contour_colorbar.svg
+++ b/lib/matplotlib/tests/baseline_images/test_axes/contour_colorbar.svg
@@ -27,7 +27,7 @@ z
" style="fill:#ffffff;"/>
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+" id="m4180484e0b" style="stroke:#000000;stroke-width:0.8;"/>
-
+
@@ -14460,7 +14460,7 @@ z
-
+
@@ -14500,7 +14500,7 @@ z
-
+
@@ -14529,7 +14529,7 @@ z
-
+
@@ -14565,7 +14565,7 @@ z
-
+
@@ -14578,7 +14578,7 @@ z
-
+
@@ -14591,7 +14591,7 @@ z
-
+
@@ -14604,7 +14604,7 @@ z
-
+
@@ -14636,7 +14636,7 @@ z
-
+
@@ -14679,10 +14679,10 @@ z
+" id="mf9bbc8b617" style="stroke:#000000;stroke-width:0.8;"/>
-
+
@@ -14696,7 +14696,7 @@ L -3.5 0
-
+
@@ -14710,7 +14710,7 @@ L -3.5 0
-
+
@@ -14724,7 +14724,7 @@ L -3.5 0
-
+
@@ -14737,7 +14737,7 @@ L -3.5 0
-
+
@@ -14750,7 +14750,7 @@ L -3.5 0
-
+
@@ -14763,7 +14763,7 @@ L -3.5 0
-
+
@@ -14776,7 +14776,7 @@ L -3.5 0
-
+
@@ -14789,7 +14789,7 @@ L -3.5 0
-
+
@@ -14803,7 +14803,7 @@ L -3.5 0
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+" id="md8bd0a4895" style="stroke:#000000;stroke-width:0.8;"/>
-
+
@@ -18083,7 +18083,7 @@ z
-
+
@@ -18099,7 +18099,7 @@ z
-
+
@@ -18156,7 +18156,7 @@ z
-
+
@@ -18172,7 +18172,7 @@ z
-
+
@@ -18187,7 +18187,7 @@ z
-
+
@@ -18202,7 +18202,7 @@ z
-
+
@@ -18217,7 +18217,7 @@ z
-
+
@@ -18232,7 +18232,7 @@ z
-
+
@@ -18246,58 +18246,58 @@ z
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
@@ -18315,10 +18315,10 @@ z
-
+
-
+
diff --git a/lib/matplotlib/tests/baseline_images/test_contour/contour_addlines.png b/lib/matplotlib/tests/baseline_images/test_contour/contour_addlines.png
new file mode 100644
index 000000000000..2115054d13b2
Binary files /dev/null and b/lib/matplotlib/tests/baseline_images/test_contour/contour_addlines.png differ
diff --git a/lib/matplotlib/tests/baseline_images/test_contour/contour_manual_colors_and_levels.png b/lib/matplotlib/tests/baseline_images/test_contour/contour_manual_colors_and_levels.png
index c4ec5f0beed4..632d1f7e7594 100644
Binary files a/lib/matplotlib/tests/baseline_images/test_contour/contour_manual_colors_and_levels.png and b/lib/matplotlib/tests/baseline_images/test_contour/contour_manual_colors_and_levels.png differ
diff --git a/lib/matplotlib/tests/test_contour.py b/lib/matplotlib/tests/test_contour.py
index 85eb2bcd81fd..f2a7b51e9bf9 100644
--- a/lib/matplotlib/tests/test_contour.py
+++ b/lib/matplotlib/tests/test_contour.py
@@ -401,3 +401,16 @@ def test_contourf_log_extension():
plt.colorbar(c1, ax=ax1)
plt.colorbar(c2, ax=ax2)
plt.colorbar(c3, ax=ax3)
+
+
+@image_comparison(baseline_images=['contour_addlines'],
+ extensions=['png'], remove_text=True, style='mpl20')
+def test_contour_addlines():
+ fig, ax = plt.subplots()
+ np.random.seed(19680812)
+ X = np.random.rand(10, 10)*10000
+ pcm = ax.pcolormesh(X)
+ # add 1000 to make colors visible...
+ cont = ax.contour(X+1000)
+ cb = fig.colorbar(pcm)
+ cb.add_lines(cont)