@@ -257,6 +257,10 @@ def _update_bbox_to_anchor(self, loc_in_canvas):
257
257
to set the fontsize alongside other font properties, use the *size*
258
258
parameter in *title_fontproperties*.
259
259
260
+ alignment : {'center', 'left', 'right'}, default: 'center'
261
+ The alignment of the legend title and the box of entries. The entries
262
+ are aligned as a single block, so that markers always lined up.
263
+
260
264
borderpad : float, default: :rc:`legend.borderpad`
261
265
The fractional whitespace inside the legend border, in font-size units.
262
266
@@ -336,6 +340,7 @@ def __init__(
336
340
frameon = None , # draw frame
337
341
handler_map = None ,
338
342
title_fontproperties = None , # properties for the legend title
343
+ alignment = "center" , # control the alignment within the legend box
339
344
* ,
340
345
ncol = 1 # synonym for ncols (backward compatibility)
341
346
):
@@ -505,6 +510,9 @@ def val_or_rc(val, rc_name):
505
510
)
506
511
self ._set_artist_props (self .legendPatch )
507
512
513
+ _api .check_in_list (["center" , "left" , "right" ], alignment = alignment )
514
+ self ._alignment = alignment
515
+
508
516
# init with null renderer
509
517
self ._init_legend_box (handles , labels , markerfirst )
510
518
@@ -804,7 +812,7 @@ def _init_legend_box(self, handles, labels, markerfirst=True):
804
812
self ._legend_title_box = TextArea ("" )
805
813
self ._legend_box = VPacker (pad = self .borderpad * fontsize ,
806
814
sep = self .labelspacing * fontsize ,
807
- align = "center" ,
815
+ align = self . _alignment ,
808
816
children = [self ._legend_title_box ,
809
817
self ._legend_handle_box ])
810
818
self ._legend_box .set_figure (self .figure )
@@ -867,10 +875,41 @@ def get_texts(self):
867
875
r"""Return the list of `~.text.Text`\s in the legend."""
868
876
return silent_list ('Text' , self .texts )
869
877
878
+ def set_alignment (self , alignment ):
879
+ """
880
+ Set the alignment of the legend title and the box of entries.
881
+
882
+ The entries are aligned as a single block, so that markers always
883
+ lined up.
884
+
885
+ Parameters
886
+ ----------
887
+ alignment : {'center', 'left', 'right'}.
888
+
889
+ """
890
+ _api .check_in_list (["center" , "left" , "right" ], alignment = alignment )
891
+ self ._alignment = alignment
892
+ self ._legend_box .align = alignment
893
+
894
+ def get_alignment (self ):
895
+ """Get the alignment value of the legend box"""
896
+ return self ._legend_box .align
897
+
870
898
def set_title (self , title , prop = None ):
871
899
"""
872
- Set the legend title. Fontproperties can be optionally set
873
- with *prop* parameter.
900
+ Set legend title and title style.
901
+
902
+ Parameters
903
+ ----------
904
+ title : str
905
+ The legend title.
906
+
907
+ prop : `.font_manager.FontProperties` or `str` or `pathlib.Path`
908
+ The font properties of the legend title.
909
+ If a `str`, it is interpreted as a fontconfig pattern parsed by
910
+ `.FontProperties`. If a `pathlib.Path`, it is interpreted as the
911
+ absolute path to a font file.
912
+
874
913
"""
875
914
self ._legend_title_box ._text .set_text (title )
876
915
if title :
0 commit comments