Skip to content

Commit 6baff85

Browse files
committed
added documentation
1 parent 36ddd26 commit 6baff85

File tree

3 files changed

+56
-8
lines changed

3 files changed

+56
-8
lines changed

lib/matplotlib/legend.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -945,7 +945,8 @@ def _init_legend_box(self, handles, labels, markerfirst=True):
945945
textbox = TextArea(lab, textprops=label_prop,
946946
multilinebaseline=True,
947947
minimumdescent=True)
948-
948+
# if handle is larger than width and height get new width and
949+
# height to fit handle else keep width and height
949950
da_width, da_height = handler.scale_dimensions(self,
950951
width, height,
951952
orig_handle)

lib/matplotlib/legend_handler.py

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,46 @@ def adjust_drawing_area(self, legend, orig_handle,
8989
return xdescent, ydescent, width, height
9090

9191
def scale_dimensions(self, legend, width, height, orig_handle):
92+
"""
93+
Return larger of input width/height or width/height of orig_handle
94+
95+
Parameters
96+
----------
97+
legend : :class:`matplotlib.legend.Legend` instance
98+
The legend that will contain the orig_handle
99+
width : number
100+
The width that is being compared to width of orig_handle.
101+
height : number
102+
The height that is being compared to height of orig_handle.
103+
orig_handle : :class:`matplotlib.artist.Artist` or similar
104+
The object that the output width and height must be large
105+
enough to fit.
106+
107+
"""
92108
return (max(width, self.handle_width(legend, orig_handle)),
93109
max(height, self.handle_height(legend, orig_handle)))
94110

95111
def handle_width(self, legend, orig_handle):
112+
"""
113+
Charles I'm not sure how to write this
114+
Return width of orig_handle.
115+
Default implementation returns -1.
116+
117+
Parameters
118+
----------
119+
legend : :class:`matplotlib.legend.Legend` instance
120+
The legend that will contain the orig_handle.
121+
orig_handle : :class:`matplotlib.artist.Artist` or similar
122+
The object that the output width and height must be large
123+
enough to fit.
124+
125+
"""
96126
return -1
97127

98128
def handle_height(self, legend, orig_handle):
129+
'''
130+
Charles
131+
'''
99132
return -1
100133

101134
def legend_artist(self, legend, orig_handle,
@@ -239,6 +272,9 @@ def __init__(self, marker_pad=0.3, numpoints=None, **kw):
239272
numpoints=numpoints, **kw)
240273

241274
def handle_width(self, legend, orig_handle):
275+
'''
276+
Charles
277+
'''
242278
if isinstance(orig_handle, Line2D):
243279
marker = orig_handle.get_marker()
244280
marker_size = orig_handle.get_markersize()
@@ -249,6 +285,9 @@ def handle_width(self, legend, orig_handle):
249285
return -1
250286

251287
def handle_height(self, legend, orig_handle):
288+
'''
289+
Charles
290+
'''
252291
if isinstance(orig_handle, Line2D):
253292
marker = orig_handle.get_marker()
254293
marker_size = orig_handle.get_markersize()
@@ -687,6 +726,9 @@ def __init__(self, ndivide=1, pad=None, **kwargs):
687726
HandlerBase.__init__(self, **kwargs)
688727

689728
def handle_width(self, legend, orig_handle):
729+
'''
730+
Charles
731+
'''
690732
handler_map = legend.get_legend_handler_map()
691733
largest_width = -1
692734
for handle1 in orig_handle:
@@ -696,12 +738,15 @@ def handle_width(self, legend, orig_handle):
696738
return largest_width
697739

698740
def handle_height(self, legend, orig_handle):
741+
'''
742+
Charles
743+
'''
699744
handler_map = legend.get_legend_handler_map()
700745
largest_height = -1
701746
for handle1 in orig_handle:
702747
handler = legend.get_legend_handler(handler_map, handle1)
703748
largest_height = max(largest_height,
704-
handler.handle_height(legend, handle1))
749+
handler.handle_height(legend, handle1))
705750
return largest_height
706751

707752
def create_artists(self, legend, orig_handle,

lib/matplotlib/tests/test_legend.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -523,29 +523,31 @@ def test_legend_title_empty():
523523
assert leg.get_title().get_text() == ""
524524
assert leg.get_title().get_visible() is False
525525

526+
526527
@image_comparison(baseline_images=['legend_large_markers'],
527528
extensions=['png'])
528529
def test_large_markers():
529530
# test that legend scales to fit markers with large markersize
530531
plt.figure()
531-
a, = plt.plot([1], [1], 's', markersize=40.)
532-
b, = plt.plot([2], [1], '*', markersize=40.)
532+
a, = plt.plot([1], [1], 's', markersize=40.)
533+
b, = plt.plot([2], [1], '*', markersize=40.)
533534
plt.legend([a, b], ["big square", "big star"], loc=2, numpoints=1)
534535

536+
535537
@image_comparison(baseline_images=['legend_large_markerscale'],
536538
extensions=['png'])
537539
def test_large_markerscale():
538540
# test that legend scales to fit markers with large markerscale
539541
plt.figure()
540542
a, = plt.plot([1], [1], 's')
541-
plt.legend([a], ["big markerscale"],markerscale=20., loc=2, numpoints=1)
543+
plt.legend([a], ["big markerscale"], markerscale=20., loc=2, numpoints=1)
544+
542545

543546
@image_comparison(baseline_images=['legend_large_marker_in_tuple'],
544547
extensions=['png'])
545548
def test_large_marker_in_tuple():
546549
# test that legend scales to fit large markers in tuple
547550
plt.figure()
548-
a, = plt.plot([1], [1], "ro",c="green", markersize=15)
549-
b, = plt.plot([1], [1], "w+",c="red", markeredgewidth=3, markersize=40)
551+
a, = plt.plot([1], [1], "ro", c="green", markersize=15)
552+
b, = plt.plot([1], [1], "w+", c="red", markeredgewidth=3, markersize=40)
550553
plt.legend([(a, b)], ["a and b"], loc=2, numpoints=1)
551-

0 commit comments

Comments
 (0)