3434import matplotlib .transforms as mtransforms
3535import matplotlib .tri as mtri
3636
37- from matplotlib .container import BarContainer
37+ from matplotlib .container import BarContainer , ErrorbarContainer
3838
3939iterable = cbook .iterable
4040is_string_like = cbook .is_string_like
@@ -1507,6 +1507,7 @@ def add_container(self, container):
15071507 if not label :
15081508 container .set_label ('_container%d' % len (self .containers ))
15091509 self .containers .append (container )
1510+ container .set_remove_method (lambda h : self .containers .remove (container ))
15101511 return container
15111512
15121513
@@ -4200,19 +4201,25 @@ def xcorr(self, x, y, normed=True, detrend=mlab.detrend_none,
42004201
42014202 def _get_legend_handles (self ):
42024203 "return artists that will be used as handles for legend"
4203- handles = self .lines [:]
4204- handles . extend ( self .patches )
4204+ handles_original = self .lines + self . patches + \
4205+ self . collections + self .containers
42054206
42064207 # collections
42074208 legend_map_keys = mlegend .Legend ._default_handler_map .keys ()
4208- collection_types = [cls for cls in legend_map_keys \
4209- if issubclass (cls , mcoll .Collection )]
4209+ # collection_types = [cls for cls in legend_map_keys \
4210+ # if issubclass(cls, mcoll.Collection)]
42104211
4211- for cls in collection_types :
4212- handles .extend ([c for c in self .collections
4213- if isinstance (c , cls )])
4212+ handles = []
4213+ for h in handles_original :
4214+ if h .get_label ().startswith ('_' ):
4215+ continue
4216+
4217+ # check subclass
4218+ for cls in legend_map_keys :
4219+ if isinstance (h , cls ):
4220+ handles .append (h )
4221+ break
42144222
4215- handles .extend ([c for c in self .containers ])
42164223 return handles
42174224
42184225
@@ -4231,8 +4238,7 @@ def get_legend_handles_labels(self):
42314238 labels = []
42324239 for handle in self ._get_legend_handles ():
42334240 label = handle .get_label ()
4234- if (label is not None and
4235- label != '' and not label .startswith ('_' )):
4241+ if (label is not None and label != '' ):
42364242 handles .append (handle )
42374243 labels .append (label )
42384244
@@ -5140,6 +5146,8 @@ def errorbar(self, x, y, yerr=None, xerr=None,
51405146 holdstate = self ._hold
51415147 self ._hold = True
51425148
5149+ label = kwargs .pop ("label" , None )
5150+
51435151 # make sure all the args are iterable; use lists not arrays to
51445152 # preserve units
51455153 if not iterable (x ):
@@ -5159,7 +5167,7 @@ def errorbar(self, x, y, yerr=None, xerr=None,
51595167 l0 = None
51605168
51615169 if barsabove and fmt is not None :
5162- l0 , = self .plot (x ,y ,fmt ,** kwargs )
5170+ l0 , = self .plot (x ,y ,fmt ,label = "_nolegend_" , ** kwargs )
51635171
51645172 barcols = []
51655173 caplines = []
@@ -5312,7 +5320,14 @@ def xywhere(xs, ys, mask):
53125320
53135321 self .autoscale_view ()
53145322 self ._hold = holdstate
5315- return (l0 , caplines , barcols )
5323+
5324+ errorbar_container = ErrorbarContainer ((l0 , tuple (caplines ), tuple (barcols )),
5325+ has_xerr = (xerr is not None ),
5326+ has_yerr = (yerr is not None ),
5327+ label = label )
5328+ self .containers .append (errorbar_container )
5329+
5330+ return errorbar_container # (l0, caplines, barcols)
53165331
53175332 def boxplot (self , x , notch = 0 , sym = 'b+' , vert = 1 , whis = 1.5 ,
53185333 positions = None , widths = None , patch_artist = False ,
0 commit comments