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

Skip to content

Commit fae2511

Browse files
committed
legend: calls handler.scale_dimensions legend_handler: added scale_dimensions method and helpers
1 parent 5df8380 commit fae2511

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

lib/matplotlib/legend.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -917,6 +917,7 @@ def _init_legend_box(self, handles, labels, markerfirst=True):
917917
descent = 0.35 * self._approx_text_height() * (self.handleheight - 0.7)
918918
# 0.35 and 0.7 are just heuristic numbers and may need to be improved.
919919
height = self._approx_text_height() * self.handleheight - descent
920+
width = self.handlelength * fontsize
920921
# each handle needs to be drawn inside a box of (x, y, w, h) =
921922
# (0, -descent, width, height). And their coordinates should
922923
# be given in the display coordinates.
@@ -929,6 +930,7 @@ def _init_legend_box(self, handles, labels, markerfirst=True):
929930

930931
for orig_handle, lab in zip(handles, labels):
931932
handler = self.get_legend_handler(legend_handler_map, orig_handle)
933+
#print(lab,"\norig handle ",type(orig_handle), "\nhandler: ", type(handler))#my change here
932934
if handler is None:
933935
warnings.warn(
934936
"Legend does not support {!r} instances.\nA proxy artist "
@@ -944,15 +946,20 @@ def _init_legend_box(self, handles, labels, markerfirst=True):
944946
textbox = TextArea(lab, textprops=label_prop,
945947
multilinebaseline=True,
946948
minimumdescent=True)
947-
handlebox = DrawingArea(width=self.handlelength * fontsize,
948-
height=height,
949+
950+
da_width, da_height = handler.scale_dimensions(self,
951+
width, height,
952+
orig_handle)
953+
handlebox = DrawingArea(width=da_width,
954+
height=da_height,
949955
xdescent=0., ydescent=descent)
950956

951957
text_list.append(textbox._text)
952958
# Create the artist for the legend which represents the
953959
# original artist/handle.
954960
handle_list.append(handler.legend_artist(self, orig_handle,
955961
fontsize, handlebox))
962+
#print("new handle: ", type(handle_list[-1]),"\n")#my change here
956963
handles_and_labels.append((handlebox, textbox))
957964

958965
if handles_and_labels:

lib/matplotlib/legend_handler.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,16 @@ def adjust_drawing_area(self, legend, orig_handle,
8888
height = height - self._ypad * fontsize
8989
return xdescent, ydescent, width, height
9090

91+
def scale_dimensions(self, legend, width, height, orig_handle):
92+
return (max(width, self.handle_width(legend, orig_handle)),
93+
max(height, self.handle_height(legend, orig_handle)))
94+
95+
def handle_width(self, legend, orig_handle):
96+
return -1
97+
98+
def handle_height(self, legend, orig_handle):
99+
return -1
100+
91101
def legend_artist(self, legend, orig_handle,
92102
fontsize, handlebox):
93103
"""
@@ -228,6 +238,26 @@ def __init__(self, marker_pad=0.3, numpoints=None, **kw):
228238
HandlerNpoints.__init__(self, marker_pad=marker_pad,
229239
numpoints=numpoints, **kw)
230240

241+
def handle_width(self, legend, orig_handle):
242+
if isinstance(orig_handle, Line2D):
243+
marker = orig_handle.get_marker()
244+
marker_size = orig_handle.get_markersize()
245+
if marker and marker_size > 0:
246+
if legend.markerscale != 1:
247+
marker_size = marker_size * legend.markerscale
248+
return marker_size
249+
return -1
250+
251+
def handle_height(self, legend, orig_handle):
252+
if isinstance(orig_handle, Line2D):
253+
marker = orig_handle.get_marker()
254+
marker_size = orig_handle.get_markersize()
255+
if marker and marker_size > 0:
256+
if legend.markerscale != 1:
257+
marker_size = marker_size * legend.markerscale
258+
return marker_size
259+
return -1
260+
231261
def create_artists(self, legend, orig_handle,
232262
xdescent, ydescent, width, height, fontsize,
233263
trans):
@@ -656,6 +686,24 @@ def __init__(self, ndivide=1, pad=None, **kwargs):
656686
self._pad = pad
657687
HandlerBase.__init__(self, **kwargs)
658688

689+
def handle_width(self, legend, orig_handle):
690+
handler_map = legend.get_legend_handler_map()
691+
largest_width = -1
692+
for handle1 in orig_handle:
693+
handler = legend.get_legend_handler(handler_map, handle1)
694+
largest_width = max(largest_width,
695+
handler.handle_width(legend, handle1))
696+
return largest_width
697+
698+
def handle_height(self, legend, orig_handle):
699+
handler_map = legend.get_legend_handler_map()
700+
largest_height = -1
701+
for handle1 in orig_handle:
702+
handler = legend.get_legend_handler(handler_map, handle1)
703+
largest_height = max(largest_height,
704+
handler.handle_height(legend, handle1))
705+
return largest_height
706+
659707
def create_artists(self, legend, orig_handle,
660708
xdescent, ydescent, width, height, fontsize,
661709
trans):

0 commit comments

Comments
 (0)