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

Skip to content

Commit d1d464c

Browse files
committed
Merge pull request matplotlib#2843 from tacaswell/contour_fix
BUGFIX: This change fixes matplotlib#2475, where contour labels added manually
2 parents 2b74cb5 + 23b5240 commit d1d464c

File tree

3 files changed

+46
-9
lines changed

3 files changed

+46
-9
lines changed

lib/matplotlib/contour.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -548,9 +548,11 @@ def add_label_clabeltext(self, x, y, rotation, lev, cvalue):
548548
def add_label_near(self, x, y, inline=True, inline_spacing=5,
549549
transform=None):
550550
"""
551-
Add a label near the point (x, y) of the given transform.
552-
If transform is None, data transform is used. If transform is
553-
False, IdentityTransform is used.
551+
Add a label near the point (x, y). If transform is None
552+
(default), (x, y) is in data coordinates; if transform is
553+
False, (x, y) is in display coordinates; otherwise, the
554+
specified transform will be used to translate (x, y) into
555+
display coordinates.
554556
555557
*inline*:
556558
controls whether the underlying contour is removed or
@@ -569,19 +571,26 @@ def add_label_near(self, x, y, inline=True, inline_spacing=5,
569571
if transform:
570572
x, y = transform.transform_point((x, y))
571573

574+
# find the nearest contour _in screen units_
572575
conmin, segmin, imin, xmin, ymin = self.find_nearest_contour(
573576
x, y, self.labelIndiceList)[:5]
574577

575578
# The calc_label_rot_and_inline routine requires that (xmin,ymin)
576579
# be a vertex in the path. So, if it isn't, add a vertex here
580+
581+
# grab the paths from the collections
577582
paths = self.collections[conmin].get_paths()
578-
lc = paths[segmin].vertices
579-
if transform:
580-
xcmin = transform.inverted().transform([xmin, ymin])
581-
else:
582-
xcmin = np.array([xmin, ymin])
583+
# grab the correct segment
584+
active_path = paths[segmin]
585+
# grab it's verticies
586+
lc = active_path.vertices
587+
# sort out where the new vertex should be added data-units
588+
xcmin = self.ax.transData.inverted().transform_point([xmin, ymin])
589+
# if there isn't a vertex close enough
583590
if not np.allclose(xcmin, lc[imin]):
591+
# insert new data into the vertex list
584592
lc = np.r_[lc[:imin], np.array(xcmin)[None, :], lc[imin:]]
593+
# replace the path with the new one
585594
paths[segmin] = mpath.Path(lc)
586595

587596
# Get index of nearest level in subset of levels used for labeling

lib/matplotlib/tests/test_contour.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import numpy as np
2-
2+
from matplotlib import mlab
33
from matplotlib.testing.decorators import cleanup, image_comparison
44
from matplotlib import pyplot as plt
55

@@ -183,6 +183,34 @@ def test_given_colors_levels_and_extends():
183183
plt.colorbar()
184184

185185

186+
@image_comparison(baseline_images=['contour_test_label_transforms'],
187+
extensions=['png'], remove_text=True)
188+
def test_labels():
189+
# Adapted from pylab_examples example code: contour_demo.py
190+
# see issues #2475, #2843, and #2818 for explanation
191+
delta = 0.025
192+
x = np.arange(-3.0, 3.0, delta)
193+
y = np.arange(-2.0, 2.0, delta)
194+
X, Y = np.meshgrid(x, y)
195+
Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
196+
Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
197+
# difference of Gaussians
198+
Z = 10.0 * (Z2 - Z1)
199+
200+
fig, ax = plt.subplots(1, 1)
201+
CS = ax.contour(X, Y, Z)
202+
disp_units = [(216, 177), (359, 290), (521, 406)]
203+
data_units = [(-2, .5), (0, -1.5), (2.8, 1)]
204+
205+
CS.clabel()
206+
207+
for x, y in data_units:
208+
CS.add_label_near(x, y, inline=True, transform=None)
209+
210+
for x, y in disp_units:
211+
CS.add_label_near(x, y, inline=True, transform=False)
212+
213+
186214
if __name__ == '__main__':
187215
import nose
188216
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)

0 commit comments

Comments
 (0)