@@ -21,6 +21,7 @@ def __init__(
2121 init_class = None ,
2222 markers = None ,
2323 colors = None ,
24+ disable_legend = None ,
2425 legend_bbox = (1.04 , 1 ),
2526 legend_loc = "upper left" ,
2627 pick_dist = 10 ,
@@ -38,6 +39,8 @@ class in *classes*
3839 markers : list
3940 The marker styles to use.
4041 colors : list
42+ disable_legend : bool
43+ disable to show the classes in the legend, default is False
4144 legend_bbox : tuple
4245 bbox to use for the legend
4346 legend_loc : str or int
@@ -50,6 +53,7 @@ class in *classes*
5053 """
5154 if classes is None :
5255 classes = 1
56+ disable_legend = True
5357
5458 if isinstance (classes , Integral ):
5559 self ._classes = list (range (classes ))
@@ -70,35 +74,47 @@ class in *classes*
7074 colors = [None ] * len (self ._classes )
7175 if markers is None :
7276 markers = ["o" ] * len (self ._classes )
77+ if disable_legend is None :
78+ disable_legend = False
79+ if disable_legend and len (self ._classes ) != 1 :
80+ disable_legend = False
81+
82+ self ._disable_legend = disable_legend
7383
7484 self .ax = ax
7585 self ._lines = {}
7686 linestyle = line_kwargs .pop ("linestyle" , "" )
7787 for i , c in enumerate (self ._classes ):
88+ label = c
89+ if disable_legend :
90+ label = None
7891 (self ._lines [c ],) = self .ax .plot (
7992 [],
8093 [],
8194 color = colors [i ],
8295 marker = markers [i ],
83- label = c ,
96+ label = label ,
8497 linestyle = linestyle ,
8598 ** line_kwargs ,
8699 )
87- self ._leg = self .ax .legend (bbox_to_anchor = legend_bbox , loc = legend_loc )
88- self ._leg_artists = {}
89- self ._class_leg_artists = {}
90- for legline , legtext , klass in zip (
91- self ._leg .get_lines (), self ._leg .get_texts (), self ._classes
92- ):
93- legline .set_picker (pick_dist )
94- legtext .set_picker (pick_dist )
95- self ._leg_artists [legtext ] = klass
96- self ._leg_artists [legline ] = klass
97- try :
98- # mpl < 3.5
99- self ._class_leg_artists [klass ] = (legline , legline ._legmarker , legtext )
100- except AttributeError :
101- self ._class_leg_artists [klass ] = (legline , legtext )
100+
101+ if not self ._disable_legend :
102+ self ._leg = self .ax .legend (bbox_to_anchor = legend_bbox , loc = legend_loc )
103+ self ._leg_artists = {}
104+ self ._class_leg_artists = {}
105+
106+ for legline , legtext , klass in zip (
107+ self ._leg .get_lines (), self ._leg .get_texts (), self ._classes
108+ ):
109+ legline .set_picker (pick_dist )
110+ legtext .set_picker (pick_dist )
111+ self ._leg_artists [legtext ] = klass
112+ self ._leg_artists [legline ] = klass
113+ try :
114+ # mpl < 3.5
115+ self ._class_leg_artists [klass ] = (legline , legline ._legmarker , legtext )
116+ except AttributeError :
117+ self ._class_leg_artists [klass ] = (legline , legtext )
102118
103119 self ._fig = self .ax .figure
104120 self ._fig .canvas .mpl_connect ("button_press_event" , self ._clicked )
@@ -144,6 +160,8 @@ def _on_pick(self, event):
144160 self ._observers .process ('class-changed' , klass )
145161
146162 def _update_legend_alpha (self ):
163+ if self ._disable_legend :
164+ return
147165 for c in self ._classes :
148166 alpha = 1 if c == self ._current_class else 0.2
149167 for a in self ._class_leg_artists [c ]:
0 commit comments