def __init__(self, fig, rect=[0.0, 0.0, 1.0, 1.0], *args, **kwargs): self.fig = fig azim = cbook.popd(kwargs, 'azim', -60) elev = cbook.popd(kwargs, 'elev', 30) self.xy_viewLim = unit_bbox() self.zz_viewLim = unit_bbox() self.xy_dataLim = unit_bbox() self.zz_dataLim = unit_bbox() # inihibit autoscale_view until the axises are defined # they can't be defined until Axes.__init__ has been called self._ready = 0 Axes.__init__(self, self.fig, rect, frameon=True, xticks=[], yticks=[], *args, **kwargs) self.M = None self._ready = 1 self.view_init(elev, azim) self.mouse_init() self.create_axes() self.set_top_view() #self.axesPatch.set_edgecolor((1,0,0,0)) self.axesPatch.set_linewidth(0) #self.axesPatch.set_facecolor((0,0,0,0)) self.fig.add_axes(self)
def __init__( self, figsize=None, # defaults to rc figure.figsize dpi=None, # defaults to rc figure.dpi facecolor=None, # defaults to rc figure.facecolor edgecolor=None, # defaults to rc figure.edgecolor linewidth=1.0, # the default linewidth of the frame frameon=True, # whether or not to draw the figure frame subplotpars=None, # default to rc ): """ figsize is a w,h tuple in inches dpi is dots per inch subplotpars is a SubplotParams instance, defaults to rc """ Artist.__init__(self) #self.set_figure(self) self._axstack = Stack() # maintain the current axes self._axobservers = [] self._seen = {} # axes args we've seen if figsize is None: figsize = rcParams['figure.figsize'] if dpi is None: dpi = rcParams['figure.dpi'] if facecolor is None: facecolor = rcParams['figure.facecolor'] if edgecolor is None: edgecolor = rcParams['figure.edgecolor'] self._unit_conversions = {} self.dpi = Value(dpi) self.figwidth = Value(figsize[0]) self.figheight = Value(figsize[1]) self.ll = Point(Value(0), Value(0)) self.ur = Point(self.figwidth * self.dpi, self.figheight * self.dpi) self.bbox = Bbox(self.ll, self.ur) self.frameon = frameon self.transFigure = get_bbox_transform(unit_bbox(), self.bbox) self.figurePatch = Rectangle( xy=(0, 0), width=1, height=1, facecolor=facecolor, edgecolor=edgecolor, linewidth=linewidth, ) self._set_artist_props(self.figurePatch) self._hold = rcParams['axes.hold'] self.canvas = None if subplotpars is None: subplotpars = SubplotParams() self.subplotpars = subplotpars self.clf() self._cachedRenderer = None
def __init__(self, figsize = None, # defaults to rc figure.figsize dpi = None, # defaults to rc figure.dpi facecolor = None, # defaults to rc figure.facecolor edgecolor = None, # defaults to rc figure.edgecolor linewidth = 1.0, # the default linewidth of the frame frameon = True, # whether or not to draw the figure frame subplotpars = None, # default to rc ): """ figsize is a w,h tuple in inches dpi is dots per inch subplotpars is a SubplotParams instance, defaults to rc """ Artist.__init__(self) #self.set_figure(self) self._axstack = Stack() # maintain the current axes self._axobservers = [] self._seen = {} # axes args we've seen if figsize is None : figsize = rcParams['figure.figsize'] if dpi is None : dpi = rcParams['figure.dpi'] if facecolor is None: facecolor = rcParams['figure.facecolor'] if edgecolor is None: edgecolor = rcParams['figure.edgecolor'] self._unit_conversions = {} self.dpi = Value(dpi) self.figwidth = Value(figsize[0]) self.figheight = Value(figsize[1]) self.ll = Point( Value(0), Value(0) ) self.ur = Point( self.figwidth*self.dpi, self.figheight*self.dpi ) self.bbox = Bbox(self.ll, self.ur) self.frameon = frameon self.transFigure = get_bbox_transform( unit_bbox(), self.bbox) self.figurePatch = Rectangle( xy=(0,0), width=1, height=1, facecolor=facecolor, edgecolor=edgecolor, linewidth=linewidth, ) self._set_artist_props(self.figurePatch) self._hold = rcParams['axes.hold'] self.canvas = None if subplotpars is None: subplotpars = SubplotParams() self.subplotpars = subplotpars self.clf() self._cachedRenderer = None
def _auto_legend_data(self): """ Returns list of vertices and extents covered by the plot. Returns a two long list. First element is a list of (x, y) vertices (in axes-coordinates) covered by all the lines and line collections, in the legend's handles. Second element is a list of bounding boxes for all the patches in the legend's handles. """ assert self.isaxes # should always hold because function is only called internally ax = self.parent vertices = [] bboxes = [] lines = [] inv = ax.transAxes.inverse_xy_tup for handle in ax.lines: assert isinstance(handle, Line2D) xdata = handle.get_xdata(orig=False) ydata = handle.get_ydata(orig=False) trans = handle.get_transform() xt, yt = trans.numerix_x_y(xdata, ydata) # XXX need a special method in transform to do a list of verts averts = [inv(v) for v in zip(xt, yt)] lines.append(averts) for handle in ax.patches: assert isinstance(handle, Patch) verts = handle.get_verts() trans = handle.get_transform() tverts = trans.seq_xy_tups(verts) averts = [inv(v) for v in tverts] bbox = unit_bbox() bbox.update(averts, True) bboxes.append(bbox) for handle in ax.collections: if isinstance(handle, LineCollection): hlines = handle._segments trans = handle.get_transform() for line in hlines: tline = trans.seq_xy_tups(line) aline = [inv(tuple(v)) for v in tline] lines.append(aline) return [vertices, bboxes, lines]
def _auto_legend_data(self): """ Returns list of vertices and extents covered by the plot. Returns a two long list. First element is a list of (x, y) vertices (in axes-coordinates) covered by all the lines and line collections, in the legend's handles. Second element is a list of bounding boxes for all the patches in the legend's handles. """ assert self.isaxes # should always hold because function is only called internally ax = self.parent vertices = [] bboxes = [] lines = [] inv = ax.transAxes.inverse_xy_tup for handle in ax.lines: assert isinstance(handle, Line2D) xdata = handle.get_xdata(orig=False) ydata = handle.get_ydata(orig=False) trans = handle.get_transform() xt, yt = trans.numerix_x_y(xdata, ydata) # XXX need a special method in transform to do a list of verts averts = [inv(v) for v in zip(xt, yt)] lines.append(averts) for handle in ax.patches: assert isinstance(handle, Patch) verts = handle.get_verts() trans = handle.get_transform() tverts = trans.seq_xy_tups(verts) averts = [inv(v) for v in tverts] bbox = unit_bbox() bbox.update(averts, True) bboxes.append(bbox) for handle in ax.collections: if isinstance(handle, LineCollection): hlines = handle.get_lines() trans = handle.get_transform() for line in hlines: tline = trans.seq_xy_tups(line) aline = [inv(v) for v in tline] lines.append(aline) return [vertices, bboxes, lines]
def __init__( self, figsize=None, # defaults to rc figure.figsize dpi=None, # defaults to rc figure.dpi facecolor=None, # defaults to rc figure.facecolor edgecolor=None, # defaults to rc figure.edgecolor linewidth=1.0, # the default linewidth of the frame frameon=True, ): """ paper size is a w,h tuple in inches DPI is dots per inch """ Artist.__init__(self) #self.set_figure(self) self._axstack = Stack() # maintain the current axes self._axobservers = [] self._seen = {} # axes args we've seen if figsize is None: figsize = rcParams['figure.figsize'] if dpi is None: dpi = rcParams['figure.dpi'] if facecolor is None: facecolor = rcParams['figure.facecolor'] if edgecolor is None: edgecolor = rcParams['figure.edgecolor'] self.dpi = Value(dpi) self.figwidth = Value(figsize[0]) self.figheight = Value(figsize[1]) self.ll = Point(Value(0), Value(0)) self.ur = Point(self.figwidth * self.dpi, self.figheight * self.dpi) self.bbox = Bbox(self.ll, self.ur) self.frameon = frameon self.transFigure = get_bbox_transform(unit_bbox(), self.bbox) self.figurePatch = Rectangle( xy=(0, 0), width=1, height=1, facecolor=facecolor, edgecolor=edgecolor, linewidth=linewidth, ) self._set_artist_props(self.figurePatch) self._hold = rcParams['axes.hold'] self.clf()
def __init__( self, figsize=None, # defaults to rc figure.figsize dpi=None, # defaults to rc figure.dpi facecolor=None, # defaults to rc figure.facecolor edgecolor=None, # defaults to rc figure.edgecolor linewidth=1.0, # the default linewidth of the frame frameon=True, ): """ paper size is a w,h tuple in inches DPI is dots per inch """ Artist.__init__(self) # self.set_figure(self) self._axstack = Stack() # maintain the current axes self._axobservers = [] self._seen = {} # axes args we've seen if figsize is None: figsize = rcParams["figure.figsize"] if dpi is None: dpi = rcParams["figure.dpi"] if facecolor is None: facecolor = rcParams["figure.facecolor"] if edgecolor is None: edgecolor = rcParams["figure.edgecolor"] self.dpi = Value(dpi) self.figwidth = Value(figsize[0]) self.figheight = Value(figsize[1]) self.ll = Point(Value(0), Value(0)) self.ur = Point(self.figwidth * self.dpi, self.figheight * self.dpi) self.bbox = Bbox(self.ll, self.ur) self.frameon = frameon self.transFigure = get_bbox_transform(unit_bbox(), self.bbox) self.figurePatch = Rectangle( xy=(0, 0), width=1, height=1, facecolor=facecolor, edgecolor=edgecolor, linewidth=linewidth ) self._set_artist_props(self.figurePatch) self._hold = rcParams["axes.hold"] self.canvas = None self.clf()
def __init__(self, figsize = None, # defaults to rc figure.figsize dpi = None, # defaults to rc figure.dpi facecolor = None, # defaults to rc figure.facecolor edgecolor = None, # defaults to rc figure.edgecolor linewidth = 1.0, # the default linewidth of the frame frameon = True, ): """ paper size is a w,h tuple in inches DPI is dots per inch """ Artist.__init__(self) #self.set_figure(self) if figsize is None : figsize = rcParams['figure.figsize'] if dpi is None : dpi = rcParams['figure.dpi'] if facecolor is None: facecolor = rcParams['figure.facecolor'] if edgecolor is None: edgecolor = rcParams['figure.edgecolor'] self.dpi = Value(dpi) self.figwidth = Value(figsize[0]) self.figheight = Value(figsize[1]) self.ll = Point( Value(0), Value(0) ) self.ur = Point( self.figwidth*self.dpi, self.figheight*self.dpi ) self.bbox = Bbox(self.ll, self.ur) self.frameon = frameon self.transFigure = get_bbox_transform( unit_bbox(), self.bbox) self.figurePatch = Rectangle( xy=(0,0), width=1, height=1, facecolor=facecolor, edgecolor=edgecolor, linewidth=linewidth, ) self._set_artist_props(self.figurePatch) self._hold = rcParams['axes.hold'] self.clf()
def __init__(self, parent, handles, labels, loc, isaxes=True): Artist.__init__(self) if is_string_like(loc) and not self.codes.has_key(loc): verbose.report_error( 'Unrecognized location %s. Falling back on upper right; valid locations are\n%s\t' % (loc, '\n\t'.join(self.codes.keys()))) if is_string_like(loc): loc = self.codes.get(loc, 1) if isaxes: # parent is an Axes self.set_figure(parent.figure) else: # parent is a Figure self.set_figure(parent) self.parent = parent self.set_transform(get_bbox_transform(unit_bbox(), parent.bbox)) self._loc = loc # make a trial box in the middle of the axes. relocate it # based on it's bbox left, upper = 0.5, 0.5 if self.NUMPOINTS == 1: self._xdata = array([left + self.HANDLELEN * 0.5]) else: self._xdata = linspace(left, left + self.HANDLELEN, self.NUMPOINTS) textleft = left + self.HANDLELEN + self.HANDLETEXTSEP self.texts = self._get_texts(labels, textleft, upper) self.handles = self._get_handles(handles, self.texts) left, top = self.texts[-1].get_position() HEIGHT = self._approx_text_height() bottom = top - HEIGHT left -= self.HANDLELEN + self.HANDLETEXTSEP + self.PAD self.legendPatch = Rectangle( xy=(left, bottom), width=0.5, height=HEIGHT * len(self.texts), facecolor='w', edgecolor='k', ) self._set_artist_props(self.legendPatch) self._drawFrame = True
def __init__(self, parent, handles, labels, loc, isaxes=True): Artist.__init__(self) if is_string_like(loc) and not self.codes.has_key(loc): verbose.report_error( "Unrecognized location %s. Falling back on upper right; valid locations are\n%s\t" % (loc, "\n\t".join(self.codes.keys())) ) if is_string_like(loc): loc = self.codes.get(loc, 1) if isaxes: # parent is an Axes self.set_figure(parent.figure) else: # parent is a Figure self.set_figure(parent) self.parent = parent self.set_transform(get_bbox_transform(unit_bbox(), parent.bbox)) self._loc = loc # make a trial box in the middle of the axes. relocate it # based on it's bbox left, upper = 0.5, 0.5 if self.NUMPOINTS == 1: self._xdata = array([left + self.HANDLELEN * 0.5]) else: self._xdata = linspace(left, left + self.HANDLELEN, self.NUMPOINTS) textleft = left + self.HANDLELEN + self.HANDLETEXTSEP self.texts = self._get_texts(labels, textleft, upper) self.handles = self._get_handles(handles, self.texts) left, top = self.texts[-1].get_position() HEIGHT = self._approx_text_height() bottom = top - HEIGHT left -= self.HANDLELEN + self.HANDLETEXTSEP + self.PAD self.legendPatch = Rectangle( xy=(left, bottom), width=0.5, height=HEIGHT * len(self.texts), facecolor="w", edgecolor="k" ) self._set_artist_props(self.legendPatch) self._drawFrame = True
def __init__(self, parent, handles, labels, loc, isaxes=True, numpoints = 4, # the number of points in the legend line prop = FontProperties(size='smaller'), pad = 0.2, # the fractional whitespace inside the legend border markerscale = 0.6, # the relative size of legend markers vs. original # the following dimensions are in axes coords labelsep = 0.005, # the vertical space between the legend entries handlelen = 0.05, # the length of the legend lines handletextsep = 0.02, # the space between the legend line and legend text axespad = 0.02, # the border between the axes and legend edge shadow=False, ): """ parent # the artist that contains the legend handles # a list of artists (lines, patches) to add to the legend labels # a list of strings to label the legend loc # a location code isaxes=True # whether this is an axes legend numpoints = 4 # the number of points in the legend line fontprop = FontProperties('smaller') # the font property pad = 0.2 # the fractional whitespace inside the legend border markerscale = 0.6 # the relative size of legend markers vs. original shadow # if True, draw a shadow behind legend The following dimensions are in axes coords labelsep = 0.005 # the vertical space between the legend entries handlelen = 0.05 # the length of the legend lines handletextsep = 0.02 # the space between the legend line and legend text axespad = 0.02 # the border between the axes and legend edge """ Artist.__init__(self) if is_string_like(loc) and not self.codes.has_key(loc): verbose.report_error('Unrecognized location %s. Falling back on upper right; valid locations are\n%s\t' %(loc, '\n\t'.join(self.codes.keys()))) if is_string_like(loc): loc = self.codes.get(loc, 1) self.numpoints = numpoints self.prop = prop self.fontsize = prop.get_size_in_points() self.pad = pad self.markerscale = markerscale self.labelsep = labelsep self.handlelen = handlelen self.handletextsep = handletextsep self.axespad = axespad self.shadow = shadow if isaxes: # parent is an Axes self.set_figure(parent.figure) else: # parent is a Figure self.set_figure(parent) self.parent = parent self.set_transform( get_bbox_transform( unit_bbox(), parent.bbox) ) self._loc = loc # make a trial box in the middle of the axes. relocate it # based on it's bbox left, upper = 0.5, 0.5 if self.numpoints == 1: self._xdata = array([left + self.handlelen*0.5]) else: self._xdata = linspace(left, left + self.handlelen, self.numpoints) textleft = left+ self.handlelen+self.handletextsep self.texts = self._get_texts(labels, textleft, upper) self.handles = self._get_handles(handles, self.texts) left, top = self.texts[-1].get_position() HEIGHT = self._approx_text_height() bottom = top-HEIGHT left -= self.handlelen + self.handletextsep + self.pad self.legendPatch = Rectangle( xy=(left, bottom), width=0.5, height=HEIGHT*len(self.texts), facecolor='w', edgecolor='k', ) self._set_artist_props(self.legendPatch) self._drawFrame = True
def _auto_legend_data(self): """ Returns list of vertices and extents covered by the plot. Returns a two long list. First element is a list of (x, y) vertices (in axes-coordinates) covered by all the lines and line collections, in the legend's handles. Second element is a list of bounding boxes for all the patches in the legend's handles. """ if not self.isaxes: raise Exception, 'Auto legends not available for figure legends.' def get_handles(ax): handles = ax.lines handles.extend(ax.patches) handles.extend([c for c in ax.collections if isinstance(c, LineCollection)]) return handles ax = self.parent handles = get_handles(ax) vertices = [] bboxes = [] lines = [] inv = ax.transAxes.inverse_xy_tup for handle in handles: if isinstance(handle, Line2D): xdata = handle.get_xdata(valid_only = True) ydata = handle.get_ydata(valid_only = True) trans = handle.get_transform() xt, yt = trans.numerix_x_y(xdata, ydata) # XXX need a special method in transform to do a list of verts averts = [inv(v) for v in zip(xt, yt)] lines.append(averts) elif isinstance(handle, Patch): verts = handle.get_verts() trans = handle.get_transform() tverts = trans.seq_xy_tups(verts) averts = [inv(v) for v in tverts] bbox = unit_bbox() bbox.update(averts, True) bboxes.append(bbox) elif isinstance(handle, LineCollection): hlines = handle.get_lines() trans = handle.get_transform() for line in hlines: tline = trans.seq_xy_tups(line) aline = [inv(v) for v in tline] lines.extend(line) return [vertices, bboxes, lines]
def __init__(self, parent, handles, labels, loc, isaxes= None, numpoints = None, # the number of points in the legend line prop = None, pad = None, # the fractional whitespace inside the legend border markerscale = None, # the relative size of legend markers vs. original # the following dimensions are in axes coords labelsep = None, # the vertical space between the legend entries handlelen = None, # the length of the legend lines handletextsep = None, # the space between the legend line and legend text axespad = None, # the border between the axes and legend edge shadow= None, ): """ parent # the artist that contains the legend handles # a list of artists (lines, patches) to add to the legend labels # a list of strings to label the legend loc # a location code isaxes=True # whether this is an axes legend numpoints = 4 # the number of points in the legend line fontprop = FontProperties(size='smaller') # the font property pad = 0.2 # the fractional whitespace inside the legend border markerscale = 0.6 # the relative size of legend markers vs. original shadow # if True, draw a shadow behind legend The following dimensions are in axes coords labelsep = 0.005 # the vertical space between the legend entries handlelen = 0.05 # the length of the legend lines handletextsep = 0.02 # the space between the legend line and legend text axespad = 0.02 # the border between the axes and legend edge """ Artist.__init__(self) if is_string_like(loc) and not self.codes.has_key(loc): warnings.warn('Unrecognized location %s. Falling back on upper right; valid locations are\n%s\t' %(loc, '\n\t'.join(self.codes.keys()))) if is_string_like(loc): loc = self.codes.get(loc, 1) proplist=[numpoints, pad, markerscale, labelsep, handlelen, handletextsep, axespad, shadow, isaxes] propnames=['numpoints', 'pad', 'markerscale', 'labelsep', 'handlelen', 'handletextsep', 'axespad', 'shadow', 'isaxes'] for name, value in zip(propnames,proplist): if value is None: value=rcParams["legend."+name] setattr(self,name,value) if prop is None: self.prop=FontProperties(size=rcParams["legend.fontsize"]) else: self.prop=prop self.fontsize = self.prop.get_size_in_points() if self.isaxes: # parent is an Axes self.set_figure(parent.figure) else: # parent is a Figure self.set_figure(parent) self.parent = parent self.set_transform( get_bbox_transform( unit_bbox(), parent.bbox) ) self._loc = loc # make a trial box in the middle of the axes. relocate it # based on it's bbox left, top = 0.5, 0.5 if self.numpoints == 1: self._xdata = array([left + self.handlelen*0.5]) else: self._xdata = linspace(left, left + self.handlelen, self.numpoints) textleft = left+ self.handlelen+self.handletextsep self.texts = self._get_texts(labels, textleft, top) self.legendHandles = self._get_handles(handles, self.texts) if len(self.texts): left, top = self.texts[-1].get_position() HEIGHT = self._approx_text_height()*len(self.texts) else: HEIGHT = 0.2 bottom = top-HEIGHT left -= self.handlelen + self.handletextsep + self.pad self.legendPatch = Rectangle( xy=(left, bottom), width=0.5, height=HEIGHT, facecolor='w', edgecolor='k', ) self._set_artist_props(self.legendPatch) self._drawFrame = True
def __init__( self, parent, handles, labels, loc=None, numpoints=None, # the number of points in the legend line prop=None, pad=None, # the fractional whitespace inside the legend border markerscale=None, # the relative size of legend markers vs. original # the following dimensions are in axes coords labelsep=None, # the vertical space between the legend entries handlelen=None, # the length of the legend lines handletextsep=None, # the space between the legend line and legend text axespad=None, # the border between the axes and legend edge shadow=None): """ parent # the artist that contains the legend handles # a list of artists (lines, patches) to add to the legend labels # a list of strings to label the legend loc # a location code numpoints = 4 # the number of points in the legend line prop = FontProperties(size='smaller') # the font property pad = 0.2 # the fractional whitespace inside the legend border markerscale = 0.6 # the relative size of legend markers vs. original shadow # if True, draw a shadow behind legend The following dimensions are in axes coords labelsep = 0.005 # the vertical space between the legend entries handlelen = 0.05 # the length of the legend lines handletextsep = 0.02 # the space between the legend line and legend text axespad = 0.02 # the border between the axes and legend edge """ from axes import Axes # local import only to avoid circularity from figure import Figure # local import only to avoid circularity Artist.__init__(self) proplist = [ numpoints, pad, markerscale, labelsep, handlelen, handletextsep, axespad, shadow ] propnames = [ 'numpoints', 'pad', 'markerscale', 'labelsep', 'handlelen', 'handletextsep', 'axespad', 'shadow' ] for name, value in zip(propnames, proplist): if value is None: value = rcParams["legend." + name] setattr(self, name, value) if prop is None: self.prop = FontProperties(size=rcParams["legend.fontsize"]) else: self.prop = prop self.fontsize = self.prop.get_size_in_points() if isinstance(parent, Axes): self.isaxes = True self.set_figure(parent.figure) elif isinstance(parent, Figure): self.isaxes = False self.set_figure(parent) else: raise TypeError("Legend needs either Axes or Figure as parent") self.parent = parent self.set_transform(get_bbox_transform(unit_bbox(), parent.bbox)) if loc is None: loc = rcParams["legend.loc"] if not self.isaxes and loc in [0, 'best']: loc = 'upper right' if is_string_like(loc): if not self.codes.has_key(loc): if self.isaxes: warnings.warn( 'Unrecognized location "%s". Falling back on "best"; ' 'valid locations are\n\t%s\n' % (loc, '\n\t'.join(self.codes.keys()))) loc = 0 else: warnings.warn( 'Unrecognized location "%s". Falling back on "upper right"; ' 'valid locations are\n\t%s\n' % (loc, '\n\t'.join(self.codes.keys()))) loc = 1 else: loc = self.codes[loc] if not self.isaxes and loc == 0: warnings.warn( 'Automatic legend placement (loc="best") not implemented for figure legend. ' 'Falling back on "upper right".') loc = 1 self._loc = loc # make a trial box in the middle of the axes. relocate it # based on it's bbox left, top = 0.5, 0.5 if self.numpoints == 1: self._xdata = npy.array([left + self.handlelen * 0.5]) else: self._xdata = npy.linspace(left, left + self.handlelen, self.numpoints) textleft = left + self.handlelen + self.handletextsep self.texts = self._get_texts(labels, textleft, top) self.legendHandles = self._get_handles(handles, self.texts) if len(self.texts): left, top = self.texts[-1].get_position() HEIGHT = self._approx_text_height() * len(self.texts) else: HEIGHT = 0.2 bottom = top - HEIGHT left -= self.handlelen + self.handletextsep + self.pad self.legendPatch = Rectangle( xy=(left, bottom), width=0.5, height=HEIGHT, facecolor='w', edgecolor='k', ) self._set_artist_props(self.legendPatch) self._drawFrame = True
def __init__(self, parent, handles, labels, loc = None, numpoints = None, # the number of points in the legend line prop = None, pad = None, # the fractional whitespace inside the legend border markerscale = None, # the relative size of legend markers vs. original # the following dimensions are in axes coords labelsep = None, # the vertical space between the legend entries handlelen = None, # the length of the legend lines handletextsep = None, # the space between the legend line and legend text axespad = None, # the border between the axes and legend edge shadow = None ): """ parent # the artist that contains the legend handles # a list of artists (lines, patches) to add to the legend labels # a list of strings to label the legend loc # a location code numpoints = 4 # the number of points in the legend line prop = FontProperties(size='smaller') # the font property pad = 0.2 # the fractional whitespace inside the legend border markerscale = 0.6 # the relative size of legend markers vs. original shadow # if True, draw a shadow behind legend The following dimensions are in axes coords labelsep = 0.005 # the vertical space between the legend entries handlelen = 0.05 # the length of the legend lines handletextsep = 0.02 # the space between the legend line and legend text axespad = 0.02 # the border between the axes and legend edge """ from axes import Axes # local import only to avoid circularity from figure import Figure # local import only to avoid circularity Artist.__init__(self) proplist=[numpoints, pad, markerscale, labelsep, handlelen, handletextsep, axespad, shadow] propnames=['numpoints', 'pad', 'markerscale', 'labelsep', 'handlelen', 'handletextsep', 'axespad', 'shadow'] for name, value in zip(propnames,proplist): if value is None: value=rcParams["legend."+name] setattr(self,name,value) if prop is None: self.prop=FontProperties(size=rcParams["legend.fontsize"]) else: self.prop=prop self.fontsize = self.prop.get_size_in_points() if isinstance(parent,Axes): self.isaxes = True self.set_figure(parent.figure) elif isinstance(parent,Figure): self.isaxes = False self.set_figure(parent) else: raise TypeError("Legend needs either Axes or Figure as parent") self.parent = parent self.set_transform( get_bbox_transform( unit_bbox(), parent.bbox) ) if loc is None: loc = rcParams["legend.loc"] if not self.isaxes and loc in [0,'best']: loc = 'upper right' if is_string_like(loc): if not self.codes.has_key(loc): if self.isaxes: warnings.warn('Unrecognized location "%s". Falling back on "best"; ' 'valid locations are\n\t%s\n' % (loc, '\n\t'.join(self.codes.keys()))) loc = 0 else: warnings.warn('Unrecognized location "%s". Falling back on "upper right"; ' 'valid locations are\n\t%s\n' % (loc, '\n\t'.join(self.codes.keys()))) loc = 1 else: loc = self.codes[loc] if not self.isaxes and loc == 0: warnings.warn('Automatic legend placement (loc="best") not implemented for figure legend. ' 'Falling back on "upper right".') loc = 1 self._loc = loc # make a trial box in the middle of the axes. relocate it # based on it's bbox left, top = 0.5, 0.5 if self.numpoints == 1: self._xdata = npy.array([left + self.handlelen*0.5]) else: self._xdata = npy.linspace(left, left + self.handlelen, self.numpoints) textleft = left+ self.handlelen+self.handletextsep self.texts = self._get_texts(labels, textleft, top) self.legendHandles = self._get_handles(handles, self.texts) if len(self.texts): left, top = self.texts[-1].get_position() HEIGHT = self._approx_text_height()*len(self.texts) else: HEIGHT = 0.2 bottom = top-HEIGHT left -= self.handlelen + self.handletextsep + self.pad self.legendPatch = Rectangle( xy=(left, bottom), width=0.5, height=HEIGHT, facecolor='w', edgecolor='k', ) self._set_artist_props(self.legendPatch) self._drawFrame = True