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

Skip to content

Commit e5e3369

Browse files
committed
implement handleheight parameter for legend. This addresses #327
1 parent ae1e230 commit e5e3369

3 files changed

Lines changed: 8 additions & 5 deletions

File tree

lib/matplotlib/legend.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ def __init__(self, parent, handles, labels,
146146
borderpad = None, # the whitespace inside the legend border
147147
labelspacing=None, #the vertical space between the legend entries
148148
handlelength=None, # the length of the legend handles
149+
handleheight=None, # the height of the legend handles
149150
handletextpad=None, # the pad between the legend handle and text
150151
borderaxespad=None, # the pad between the axes and legend border
151152
columnspacing=None, # spacing between columns
@@ -184,6 +185,7 @@ def __init__(self, parent, handles, labels,
184185
borderpad the fractional whitespace inside the legend border
185186
labelspacing the vertical space between the legend entries
186187
handlelength the length of the legend handles
188+
handleheight the length of the legend handles
187189
handletextpad the pad between the legend handle and text
188190
borderaxespad the pad between the axes and legend border
189191
columnspacing the spacing between columns
@@ -223,7 +225,7 @@ def __init__(self, parent, handles, labels,
223225
self._fontsize = self.prop.get_size_in_points()
224226

225227
propnames=['numpoints', 'markerscale', 'shadow', "columnspacing",
226-
"scatterpoints"]
228+
"scatterpoints", "handleheight"]
227229

228230
self.texts = []
229231
self.legendHandles = []
@@ -585,9 +587,9 @@ def _init_legend_box(self, handles, labels):
585587

586588
# The approximate height and descent of text. These values are
587589
# only used for plotting the legend handle.
588-
height = self._approx_text_height() * 0.7
589-
descent = 0.
590-
590+
descent = 0.35*self._approx_text_height()*(self.handleheight - 0.7)
591+
# 0.35 and 0.7 are just heuristic numbers. this may need to be improbed
592+
height = self._approx_text_height() * self.handleheight - descent
591593
# each handle needs to be drawn inside a box of (x, y, w, h) =
592594
# (0, -descent, width, height). And their corrdinates should
593595
# be given in the display coordinates.

lib/matplotlib/legend_handler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ def _create_patch(self, legend, orig_handle,
231231
xdescent, ydescent, width, height, fontsize):
232232
if self._patch_func is None:
233233
p = Rectangle(xy=(-xdescent, -ydescent),
234-
width = width+xdescent, height=(height+ydescent))
234+
width = width, height=height)
235235
else:
236236
p = self._patch_func(legend=legend, orig_handle=orig_handle,
237237
xdescent=xdescent, ydescent=ydescent,

lib/matplotlib/rcsetup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,7 @@ def __call__(self, s):
472472
'legend.borderpad' : [0.4, validate_float], # units are fontsize
473473
'legend.labelspacing' : [0.5, validate_float], # the vertical space between the legend entries
474474
'legend.handlelength' : [2., validate_float], # the length of the legend lines
475+
'legend.handleheight' : [0.7, validate_float], # the length of the legend lines
475476
'legend.handletextpad' : [.8, validate_float], # the space between the legend line and legend text
476477
'legend.borderaxespad' : [0.5, validate_float], # the border between the axes and legend edge
477478
'legend.columnspacing' : [2., validate_float], # the border between the axes and legend edge

0 commit comments

Comments
 (0)