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

Skip to content

Commit 9cb6472

Browse files
committed
Introduced UniformLegend class and associated handlers.
TODO: Modify axes.legend and axes.figlegend to use UniformLegend when uniform size is desired.
1 parent 6586369 commit 9cb6472

File tree

2 files changed

+147
-1
lines changed

2 files changed

+147
-1
lines changed

lib/matplotlib/legend.py

100644100755
Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,7 @@ def get_title(self):
803803
return self._legend_title_box._text
804804

805805
def get_window_extent(self, *args, **kwargs):
806-
'return a extent of the legend'
806+
'return a extent of the the legend'
807807
return self.legendPatch.get_window_extent(*args, **kwargs)
808808

809809
def get_frame_on(self):
@@ -993,3 +993,100 @@ def draggable(self, state=None, use_blit=False, update="loc"):
993993
self._draggable = None
994994

995995
return self._draggable
996+
997+
998+
class UniformLegend(Legend):
999+
def __str__(self):
1000+
return "uniformLegend"
1001+
1002+
def __init__(self, parent, handles, labels,uniform_size,
1003+
loc=None,
1004+
numpoints=None, # the number of points in the legend line
1005+
markerfirst=True, # controls ordering (left-to-right) of
1006+
# legend marker and label
1007+
scatterpoints=None, # number of scatter points
1008+
scatteryoffsets=None,
1009+
prop=None, # properties for the legend texts
1010+
fontsize=None, # keyword to set font size directly
1011+
1012+
# spacing & pad defined as a fraction of the font-size
1013+
borderpad=None, # the whitespace inside the legend border
1014+
labelspacing=None, # the vertical space between the legend
1015+
# entries
1016+
handlelength=None, # the length of the legend handles
1017+
handleheight=None, # the height of the legend handles
1018+
handletextpad=None, # the pad between the legend handle
1019+
# and text
1020+
borderaxespad=None, # the pad between the axes and legend
1021+
# border
1022+
columnspacing=None, # spacing between columns
1023+
1024+
ncol=1, # number of columns
1025+
mode=None, # mode for horizontal distribution of columns.
1026+
# None, "expand"
1027+
1028+
fancybox=None, # True use a fancy box, false use a rounded
1029+
# box, none use rc
1030+
shadow=None,
1031+
title=None, # set a title for the legend
1032+
1033+
framealpha=None, # set frame alpha
1034+
1035+
bbox_to_anchor=None, # bbox that the legend will be anchored.
1036+
bbox_transform=None, # transform for the bbox
1037+
frameon=None
1038+
):
1039+
uniformHandlerMap = {
1040+
StemContainer:
1041+
legend_handler.HandlerUniformStem(uniform_size=uniform_size),
1042+
ErrorbarContainer:
1043+
legend_handler.HandlerUniformErrorBar(uniform_size=uniform_size),
1044+
Line2D: legend_handler.HandlerUniformLine2D(uniform_size=uniform_size),
1045+
PathCollection:
1046+
legend_handler.HandlerPathCollection(sizes=[uniform_size]*3),
1047+
RegularPolyCollection:
1048+
legend_handler.HandlerRegularPolyCollection(sizes=[uniform_size]*3),
1049+
CircleCollection:
1050+
legend_handler.HandlerCircleCollection(sizes=[uniform_size]*3),
1051+
}
1052+
1053+
Legend.__init__(self,parent,handles,labels,
1054+
loc=loc,
1055+
numpoints=numpoints, # the number of points in the legend line
1056+
markerscale=None, # the relative size of legend markers
1057+
# vs. original
1058+
markerfirst=markerfirst, # controls ordering (left-to-right) of
1059+
# legend marker and label
1060+
scatterpoints=scatterpoints, # number of scatter points
1061+
scatteryoffsets=scatteryoffsets,
1062+
prop=prop, # properties for the legend texts
1063+
fontsize=fontsize, # keyword to set font size directly
1064+
1065+
# spacing & pad defined as a fraction of the font-size
1066+
borderpad=borderpad, # the whitespace inside the legend border
1067+
labelspacing=labelspacing, # the vertical space between the legend
1068+
# entries
1069+
handlelength=handlelength, # the length of the legend handles
1070+
handleheight=handleheight, # the height of the legend handles
1071+
handletextpad=handletextpad, # the pad between the legend handle
1072+
# and text
1073+
borderaxespad=borderaxespad, # the pad between the axes and legend
1074+
# border
1075+
columnspacing=columnspacing, # spacing between columns
1076+
1077+
ncol=1, # number of columns
1078+
mode=mode, # mode for horizontal distribution of columns.
1079+
# None, "expand"
1080+
1081+
fancybox=fancybox, # True use a fancy box, false use a rounded
1082+
# box, none use rc
1083+
shadow=shadow,
1084+
title=title, # set a title for the legend
1085+
1086+
framealpha=framealpha, # set frame alpha
1087+
1088+
bbox_to_anchor=bbox_to_anchor, # bbox that the legend will be anchored.
1089+
bbox_transform=bbox_transform, # transform for the bbox
1090+
frameon=frameon, # draw frame
1091+
handler_map=uniformHandlerMap,
1092+
)

lib/matplotlib/legend_handler.py

100644100755
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,3 +617,52 @@ def create_artists(self, legend, orig_handle,
617617
self.update_prop(p, orig_handle, legend)
618618
p.set_transform(trans)
619619
return [p]
620+
class HandlerUniformLine2D(HandlerLine2D):
621+
"""
622+
Handler for Uniform sized Line2D instances
623+
"""
624+
625+
def __init__(self, uniform_size, **kw):
626+
self._uniform_size = uniform_size
627+
HandlerLine2D.__init__(self, **kw)
628+
629+
def create_artists(self, legend, orig_handle,
630+
xdescent, ydescent, width, height, fontsize,
631+
trans):
632+
artists = HandlerLine2D.create_artists(self, legend, orig_handle,
633+
xdescent, ydescent, width, height, fontsize,
634+
trans)
635+
artists[-1].set_markersize(self._uniform_size)
636+
return artists
637+
class HandlerUniformErrorBar(HandlerErrorbar):
638+
"""
639+
Handler for Uniform sized Error instances
640+
"""
641+
642+
def __init__(self, uniform_size, **kw):
643+
self._uniform_size = uniform_size
644+
HandlerErrorbar.__init__(self, **kw)
645+
646+
def create_artists(self, legend, orig_handle,
647+
xdescent, ydescent, width, height, fontsize,
648+
trans):
649+
artists = HandlerErrorbar.create_artists(self, legend, orig_handle,
650+
xdescent, ydescent, width, height, fontsize,
651+
trans)
652+
artists[-1].set_markersize(self._uniform_size)
653+
return artists
654+
class HandlerUniformStem(HandlerStem):
655+
def __init__(self, uniform_size, **kw):
656+
657+
HandlerStem.__init__(self, **kw)
658+
self._uniform_size = uniform_size
659+
660+
def create_artists(self, legend, orig_handle,
661+
xdescent, ydescent, width, height, fontsize,
662+
trans):
663+
artists = HandlerStem.create_artists(self, legend, orig_handle,
664+
xdescent, ydescent,
665+
width, height, fontsize,
666+
trans)
667+
artists[0].set_markersize(self._uniform_size)
668+
return artists

0 commit comments

Comments
 (0)