Skip to content

Commit 4af901c

Browse files
committed
Merge branch 'fix_legend_loc_best' of git://github.com/maxalbert/matplotlib into maxalbert-fix_legend_loc_best
Conflicts: lib/matplotlib/tests/baseline_images/test_legend/legend_various_labels.pdf
2 parents 38650ad + d179f4b commit 4af901c

File tree

10 files changed

+653
-18
lines changed

10 files changed

+653
-18
lines changed

doc/devel/testing.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ example, here is a test from :mod:`matplotlib.tests.test_basic`::
7373
Nose determines which functions are tests by searching for functions
7474
beginning with "test" in their name.
7575

76-
If the test as side effects that need to be cleaned up, such as
76+
If the test has side effects that need to be cleaned up, such as
7777
creating figures using the pyplot interface, use the ``@cleanup``
7878
decorator::
7979

lib/matplotlib/legend.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ class Legend(Artist):
108108
'upper center' : 9,
109109
'center' : 10,
110110
111-
loc can be a tuple of the noramilzed coordinate values with
111+
loc can be a tuple of the normalized coordinate values with
112112
respect its parent.
113113
114114
"""
@@ -736,7 +736,6 @@ def _auto_legend_data(self):
736736
assert self.isaxes
737737

738738
ax = self.parent
739-
vertices = []
740739
bboxes = []
741740
lines = []
742741

@@ -757,6 +756,11 @@ def _auto_legend_data(self):
757756
transform = handle.get_transform()
758757
bboxes.append(handle.get_path().get_extents(transform))
759758

759+
try:
760+
vertices = np.concatenate([l.vertices for l in lines])
761+
except ValueError:
762+
vertices = np.array([])
763+
760764
return [vertices, bboxes, lines]
761765

762766
def draw_frame(self, b):
@@ -922,20 +926,31 @@ def _find_best_position(self, width, height, renderer, consider=None):
922926
verts, bboxes, lines = self._auto_legend_data()
923927

924928
bbox = Bbox.from_bounds(0, 0, width, height)
925-
consider = [self._get_anchored_bbox(x, bbox, self.get_bbox_to_anchor(),
926-
renderer)
927-
for x
928-
in range(1, len(self.codes))]
929+
if consider is None:
930+
consider = [self._get_anchored_bbox(x, bbox,
931+
self.get_bbox_to_anchor(),
932+
renderer)
933+
for x in range(1, len(self.codes))]
929934

930935
#tx, ty = self.legendPatch.get_x(), self.legendPatch.get_y()
931936

932937
candidates = []
933938
for l, b in consider:
934939
legendBox = Bbox.from_bounds(l, b, width, height)
935940
badness = 0
941+
# XXX TODO: If markers are present, it would be good to
942+
# take their into account when checking vertex overlaps in
943+
# the next line.
936944
badness = legendBox.count_contains(verts)
937945
badness += legendBox.count_overlaps(bboxes)
938946
for line in lines:
947+
# FIXME: the following line is ill-suited for lines
948+
# that 'spiral' around the center, because the bbox
949+
# may intersect with the legend even if the line
950+
# itself doesn't. One solution would be to break up
951+
# the line into its straight-segment components, but
952+
# this may (or may not) result in a significant
953+
# slowdown if lines with many vertices are present.
939954
if line.intersects_bbox(legendBox):
940955
badness += 1
941956

6.76 KB
Binary file not shown.
6.78 KB
Loading

0 commit comments

Comments
 (0)