Skip to content

Commit 1f92b29

Browse files
committed
Merge remote-tracking branch 'refs/remotes/upstream/v1.3.x'
Conflicts due to tests being added by both branches Conflicts: lib/matplotlib/tests/test_contour.py
2 parents 894f3df + d1d464c commit 1f92b29

File tree

3 files changed

+47
-9
lines changed

3 files changed

+47
-9
lines changed

Diff for: lib/matplotlib/contour.py

+17-8
Original file line numberDiff line numberDiff line change
@@ -553,9 +553,11 @@ def add_label_clabeltext(self, x, y, rotation, lev, cvalue):
553553
def add_label_near(self, x, y, inline=True, inline_spacing=5,
554554
transform=None):
555555
"""
556-
Add a label near the point (x, y) of the given transform.
557-
If transform is None, data transform is used. If transform is
558-
False, IdentityTransform is used.
556+
Add a label near the point (x, y). If transform is None
557+
(default), (x, y) is in data coordinates; if transform is
558+
False, (x, y) is in display coordinates; otherwise, the
559+
specified transform will be used to translate (x, y) into
560+
display coordinates.
559561
560562
*inline*:
561563
controls whether the underlying contour is removed or
@@ -574,19 +576,26 @@ def add_label_near(self, x, y, inline=True, inline_spacing=5,
574576
if transform:
575577
x, y = transform.transform_point((x, y))
576578

579+
# find the nearest contour _in screen units_
577580
conmin, segmin, imin, xmin, ymin = self.find_nearest_contour(
578581
x, y, self.labelIndiceList)[:5]
579582

580583
# The calc_label_rot_and_inline routine requires that (xmin,ymin)
581584
# be a vertex in the path. So, if it isn't, add a vertex here
585+
586+
# grab the paths from the collections
582587
paths = self.collections[conmin].get_paths()
583-
lc = paths[segmin].vertices
584-
if transform:
585-
xcmin = transform.inverted().transform([xmin, ymin])
586-
else:
587-
xcmin = np.array([xmin, ymin])
588+
# grab the correct segment
589+
active_path = paths[segmin]
590+
# grab it's verticies
591+
lc = active_path.vertices
592+
# sort out where the new vertex should be added data-units
593+
xcmin = self.ax.transData.inverted().transform_point([xmin, ymin])
594+
# if there isn't a vertex close enough
588595
if not np.allclose(xcmin, lc[imin]):
596+
# insert new data into the vertex list
589597
lc = np.r_[lc[:imin], np.array(xcmin)[None, :], lc[imin:]]
598+
# replace the path with the new one
590599
paths[segmin] = mpath.Path(lc)
591600

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

Diff for: lib/matplotlib/tests/test_contour.py

+30-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import datetime
77

88
import numpy as np
9-
9+
from matplotlib import mlab
1010
from matplotlib.testing.decorators import cleanup, image_comparison
1111
from matplotlib import pyplot as plt
1212

@@ -192,6 +192,7 @@ def test_given_colors_levels_and_extends():
192192
plt.colorbar()
193193

194194

195+
195196
@image_comparison(baseline_images=['contour_datetime_axis'],
196197
extensions=['png'], remove_text=False)
197198
def test_contour_datetime_axis():
@@ -218,6 +219,34 @@ def test_contour_datetime_axis():
218219
label.set_rotation(30)
219220

220221

222+
@image_comparison(baseline_images=['contour_test_label_transforms'],
223+
extensions=['png'], remove_text=True)
224+
def test_labels():
225+
# Adapted from pylab_examples example code: contour_demo.py
226+
# see issues #2475, #2843, and #2818 for explanation
227+
delta = 0.025
228+
x = np.arange(-3.0, 3.0, delta)
229+
y = np.arange(-2.0, 2.0, delta)
230+
X, Y = np.meshgrid(x, y)
231+
Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
232+
Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
233+
# difference of Gaussians
234+
Z = 10.0 * (Z2 - Z1)
235+
236+
fig, ax = plt.subplots(1, 1)
237+
CS = ax.contour(X, Y, Z)
238+
disp_units = [(216, 177), (359, 290), (521, 406)]
239+
data_units = [(-2, .5), (0, -1.5), (2.8, 1)]
240+
241+
CS.clabel()
242+
243+
for x, y in data_units:
244+
CS.add_label_near(x, y, inline=True, transform=None)
245+
246+
for x, y in disp_units:
247+
CS.add_label_near(x, y, inline=True, transform=False)
248+
249+
221250
if __name__ == '__main__':
222251
import nose
223252
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)

0 commit comments

Comments
 (0)