def __init__(self, transform, sizex=0, sizey=0, labelx=None, labely=None, loc=3, pad=0.1, borderpad=0.1, sep=2, prop=None, **kwargs): """ Draw a horizontal and/or vertical bar with the size in data coordinate of the give axes. A label will be drawn underneath (center-aligned). - transform : the coordinate frame (typically axes.transData) - sizex,sizey : width of x,y bar, in data units. 0 to omit - labelx,labely : labels for x,y bars; None to omit - loc : position in containing axes - pad, borderpad : padding, in fraction of legend font size (or prop) - sep : separation between labels and bars in points. - **kwargs : additional arguments passed to base class constructor """ from matplotlib.patches import Rectangle from matplotlib.offsetbox import AuxTransformBox, VPacker, HPacker from matplotlib.offsetbox import TextArea, DrawingArea bars = AuxTransformBox(transform) if sizex: bars.add_artist(Rectangle((0, 0), sizex, 0, fc="none")) if sizey: bars.add_artist(Rectangle((0, 0), 0, sizey, fc="none")) if sizex and labelx: bars = VPacker(children=[bars, TextArea(labelx, minimumdescent=False)], align="center", pad=0, sep=sep) if sizey and labely: bars = HPacker(children=[TextArea(labely), bars], align="center", pad=0, sep=sep) AnchoredOffsetbox.__init__(self, loc, pad=pad, borderpad=borderpad, child=bars, prop=prop, frameon=False, **kwargs)
def __init__(self, transform, size, label, loc, pad=0.1, borderpad=0.1, sep=2, prop=None, frameon=True): """ Draw a horizontal bar with the size in data coordinate of the give axes. A label will be drawn underneath (center-aligned). pad, borderpad in fraction of the legend font size (or prop) sep in points. loc: 'upper right' : 1, 'upper left' : 2, 'lower left' : 3, 'lower right' : 4, 'right' : 5, 'center left' : 6, 'center right' : 7, 'lower center' : 8, 'upper center' : 9, 'center' : 10 """ self.size_bar = AuxTransformBox(transform) self.size_bar.add_artist(Rectangle((0, 0), size, 0, fc='none', color='white', lw=3)) self.txt_label = TextArea(label, dict(color='white', size='x-large', weight='normal'), minimumdescent=False) self._box = VPacker(children=[self.size_bar, self.txt_label], align="center", pad=0, sep=sep) AnchoredOffsetbox.__init__(self, loc, pad=pad, borderpad=borderpad, child=self._box, prop=prop, frameon=frameon)
def __init__(self, transform, loc, pad=0.4, borderpad=0.5, prop=None, frameon=True, **kwargs): self.drawing_area = AuxTransformBox(transform) AnchoredOffsetbox.__init__( self, loc, pad=pad, borderpad=borderpad, child=self.drawing_area, prop=prop, frameon=frameon, **kwargs )
def __init__(self, transform, fig_transform, sizex=0, sizey=0, labelx=None, labely=None, loc=4, xbar_width = 2, ybar_width = 2, pad=3, borderpad=0.1, xsep=3, ysep = 3, prop=None, textprops={'size':10}, **kwargs): """ Draw a horizontal and/or vertical bar with the size in data coordinate of the give axes. A label will be drawn underneath (center-aligned). - transform : the coordinate frame (typically axes.transData) - sizex,sizey : width of x,y bar, in data units. 0 to omit - labelx,labely : labels for x,y bars; None to omit - loc : position in containing axes - pad, borderpad : padding, in fraction of the legend font size (or prop) - sep : separation between labels and bars in points. - **kwargs : additional arguments passed to base class constructor """ from matplotlib.patches import Rectangle from matplotlib.offsetbox import AuxTransformBox, VPacker, HPacker, TextArea, DrawingArea # new shit # try splitting the transform into X and Y so that import matplotlib.transforms as transforms xtransform = transforms.blended_transform_factory(transform, fig_transform) ytransform = transforms.blended_transform_factory(fig_transform, transform) # end new shit # bars = AuxTransformBox(xtransform) # if sizey: # bars.add_artist(Rectangle((0,0), ybar_width, sizey, # fc="Black")) # if sizex: # bars.add_artist(Rectangle((0,0), sizex, xbar_width, # fc="Black")) ybar_width /= 72. xbar_width /= 72. if sizey: ybar = AuxTransformBox(ytransform) ybar.add_artist(Rectangle((0,0), ybar_width, sizey, fc="Black")) bars = ybar if sizex: xbar = AuxTransformBox(xtransform) xbar.add_artist(Rectangle((0,0), sizex, xbar_width, fc="Black")) bars = xbar if sizex and sizey: bars = VPacker(children=[ybar, xbar], pad = 10, sep=ysep) if sizex and labelx: bars = VPacker(children=[bars, TextArea(labelx, minimumdescent=False, textprops = textprops)], align="center", pad=0, sep=-3) if sizey and labely: bars = HPacker(children=[TextArea(labely, textprops = textprops), bars], align="center", pad=0, sep=xsep) AnchoredOffsetbox.__init__(self, loc, pad=pad, borderpad=borderpad, child=bars, prop=prop, frameon=False, **kwargs)
def __init__(self, transform, width, height, angle, loc, pad=0.1, borderpad=0.1, prop=None, frameon=True): """ Draw an ellipse the size in data coordinate of the give axes. pad, borderpad in fraction of the legend font size (or prop) """ self._box = AuxTransformBox(transform) self.ellipse = Ellipse((0, 0), width, height, angle) self._box.add_artist(self.ellipse) AnchoredOffsetbox.__init__(self, loc, pad=pad, borderpad=borderpad, child=self._box, prop=prop, frameon=frameon)
def __init__(self, ax, transSky2Pix, loc, arrow_fraction=0.15, txt1="E", txt2="N", delta_a1=0, delta_a2=0, pad=0.1, borderpad=0.5, prop=None, frameon=False, ): """ Draw an arrows pointing the directions of E & N arrow_fraction : length of the arrow as a fraction of axes size pad, borderpad in fraction of the legend font size (or prop) """ self._ax = ax self._transSky2Pix = transSky2Pix self._box = AuxTransformBox(ax.transData) self.delta_a1, self.delta_a2 = delta_a1, delta_a2 self.arrow_fraction = arrow_fraction kwargs = dict(mutation_scale=11, shrinkA=0, shrinkB=5) self.arrow1 = FancyArrowPatch(posA=(0, 0), posB=(1, 1), arrowstyle="->", arrow_transmuter=None, connectionstyle="arc3", connector=None, **kwargs) self.arrow2 = FancyArrowPatch(posA=(0, 0), posB=(1, 1), arrowstyle="->", arrow_transmuter=None, connectionstyle="arc3", connector=None, **kwargs) x1t, y1t, x2t, y2t = 1, 1, 1, 1 self.txt1 = Text(x1t, y1t, txt1, rotation=0, rotation_mode="anchor", va="center", ha="right") self.txt2 = Text(x2t, y2t, txt2, rotation=0, rotation_mode="anchor", va="bottom", ha="center") self._box.add_artist(self.arrow1) self._box.add_artist(self.arrow2) self._box.add_artist(self.txt1) self._box.add_artist(self.txt2) AnchoredOffsetbox.__init__(self, loc, pad=pad, borderpad=borderpad, child=self._box, prop=prop, frameon=frameon)
def __init__( self, transform, sizex=0, sizey=0, labelx=None, labely=None, loc=4, pad=0.1, borderpad=0.1, sep=2, prop=None, label_fontsize=label_fontsize, color="k", **kwargs ): """ Draw a horizontal and/or vertical bar with the size in data coordinate of the give axes. A label will be drawn underneath (center-aligned). - transform : the coordinate frame (typically axes.transData) - sizex,sizey : width of x,y bar, in data units. 0 to omit - labelx,labely : labels for x,y bars; None to omit - loc : position in containing axes - pad, borderpad : padding, in fraction of the legend font size (or prop) - sep : separation between labels and bars in points. - **kwargs : additional arguments passed to base class constructor """ from matplotlib.patches import Rectangle from matplotlib.offsetbox import AuxTransformBox, VPacker, HPacker, TextArea, DrawingArea bars = AuxTransformBox(transform) if sizex: bars.add_artist(Rectangle((0, 0), sizex, 0, fc="none", linewidth=axes_linewidth, color=color)) if sizey: bars.add_artist(Rectangle((0, 0), 0, sizey, fc="none", linewidth=axes_linewidth, color=color)) if sizex and labelx: textareax = TextArea(labelx, minimumdescent=False, textprops=dict(size=label_fontsize, color=color)) bars = VPacker(children=[bars, textareax], align="center", pad=0, sep=sep) if sizey and labely: ## VPack a padstr below the rotated labely, else label y goes below the scale bar ## Just adding spaces before labely doesn't work! padstr = "\n " * len(labely) textareafiller = TextArea(padstr, textprops=dict(size=label_fontsize / 3.0)) textareay = TextArea(labely, textprops=dict(size=label_fontsize, rotation="vertical", color=color)) ## filler / pad string VPack-ed below labely textareayoffset = VPacker(children=[textareay, textareafiller], align="center", pad=0, sep=sep) ## now HPack this padded labely to the bars bars = HPacker(children=[textareayoffset, bars], align="top", pad=0, sep=sep) AnchoredOffsetbox.__init__( self, loc, pad=pad, borderpad=borderpad, child=bars, prop=prop, frameon=False, **kwargs )
def __init__(self, transform, size, label, loc, pad=0.1, borderpad=0.1, sep=2, prop=None, frameon=True): """ Draw a horizontal bar with the size in data coordinate of the give axes. A label will be drawn underneath (center-alinged). pad, borderpad in fraction of the legend font size (or prop) sep in points. """ self.size_bar = AuxTransformBox(transform) self.size_bar.add_artist(Rectangle((0, 0), size, 0, fc="none")) self.txt_label = TextArea(label, minimumdescent=False) self._box = VPacker(children=[self.size_bar, self.txt_label], align="center", pad=0, sep=sep) AnchoredOffsetbox.__init__(self, loc, pad=pad, borderpad=borderpad, child=self._box, prop=prop, frameon=frameon)
def __init__(self, transform, sizex=0, sizey=0, labelx=None, labely=None, loc=4, pad=0.1, borderpad=0.1, sep=2, lw=2, prop=None, **kwargs): """ Draw a horizontal and/or vertical bar with the size in data coordinate of the give axes. A label will be drawn underneath (center-aligned). - transform : the coordinate frame (typically axes.transData) - sizex,sizey : width of x,y bar, in data units. 0 to omit - labelx,labely : labels for x,y bars; None to omit - loc : position in containing axes - pad, borderpad : padding, in fraction of the legend font size (or prop) - sep : separation between labels and bars in points. - **kwargs : additional arguments passed to base class constructor """ from matplotlib.patches import Rectangle from matplotlib.offsetbox import AuxTransformBox, VPacker, HPacker, TextArea, DrawingArea bars = AuxTransformBox(transform) if sizex: bars.add_artist(Rectangle((0, 0), sizex, 0, fc="none", lw=lw)) if sizey: bars.add_artist(Rectangle((0, 0), 0, sizey, fc="none", lw=lw)) if sizex and labelx: bars = VPacker( children=[bars, TextArea(labelx, minimumdescent=False)], align="center", pad=0, sep=sep) if sizey and labely: bars = HPacker(children=[TextArea(labely), bars], align="center", pad=0, sep=sep) AnchoredOffsetbox.__init__(self, loc, pad=pad, borderpad=borderpad, child=bars, prop=prop, frameon=False, **kwargs)
def __init__(self, transform, size, label, loc, pad=0.1, borderpad=0.1, sep=2, prop=None, frameon=True): """ Draw a horizontal bar with the size in data coordinate of the give axes. A label will be drawn underneath (center-aligned). pad, borderpad in fraction of the legend font size (or prop) sep in points. loc: 'upper right' : 1, 'upper left' : 2, 'lower left' : 3, 'lower right' : 4, 'right' : 5, 'center left' : 6, 'center right' : 7, 'lower center' : 8, 'upper center' : 9, 'center' : 10 """ self.size_bar = AuxTransformBox(transform) self.size_bar.add_artist( Rectangle((0, 0), size, 0, fc='none', color='white', lw=3)) self.txt_label = TextArea(label, dict(color='white', size='x-large', weight='normal'), minimumdescent=False) self._box = VPacker(children=[self.size_bar, self.txt_label], align="center", pad=0, sep=sep) AnchoredOffsetbox.__init__(self, loc, pad=pad, borderpad=borderpad, child=self._box, prop=prop, frameon=frameon)
def __init__(self, ax, label, bar_length, **props): ''' Draw a horizontal bar with the size in data coordinate of the give axes. A label will be drawn above (center-aligned). ''' label_size = props['label_size'] if 'label_size' in props else \ rcParams.get('scalebar.label_size', 16) label_family = props['label_family'] if 'label_family' in props else \ rcParams.get('scalebar.label_family', 'sans-serif') label_color = props['label_color'] if 'label_color' in props else \ rcParams.get('scalebar.label_color', 'black') location = props['location'] if 'location' in props else \ rcParams.get('scalebar.location', 4) padding = props['padding'] if 'padding' in props else \ rcParams.get('scalebar.padding', 0.5) sep = props['sep'] if 'sep' in props else \ rcParams.get('scalebar.sep', 2) bar_color = props['bar_color'] if 'bar_color' in props else \ rcParams.get('scalebar.bar_color', 'black') bar_width = props['bar_width'] if 'bar_width' in props else \ rcParams.get('scalebar.bar_width', 0.1) bar_length = props['bar_length'] if 'bar_length' in props else \ rcParams.get('scalebar.bar_length', 0.8) frameon = False prop = None self.scale_bar = AuxTransformBox(ax.transData) rect = mpatches.Rectangle((0, 0), bar_length, bar_width, linewidth=0, edgecolor=None, facecolor=bar_color) self.scale_bar.add_artist(rect) textprops = {'size': label_size} self.txt_label = TextArea(label, textprops=textprops, minimumdescent=False) self._box = VPacker(children=[self.txt_label, self.scale_bar], align="center", pad=0, sep=sep) AnchoredOffsetbox.__init__(self, location, pad=padding, borderpad=0, child=self._box, prop=prop, frameon=frameon)
def __init__(self, transform, width, height, angle, loc, pad=0.1, borderpad=0.1, prop=None, frameon=True): """ Draw an ellipse the size in data coordinate of the give axes. pad, borderpad in fraction of the legend font size (or prop) """ self._box = AuxTransformBox(transform) self.ellipse = Ellipse((0,0), width, height, angle) self._box.add_artist(self.ellipse) AnchoredOffsetbox.__init__(self, loc, pad=pad, borderpad=borderpad, child=self._box, prop=prop, frameon=frameon)
def __init__(self, transform, width, height, loc, pad=0.1, borderpad=0.1, prop=None, frameon=False, color="white"): """ Draw a rectangle the size in data coordinate of the give axes. pad, borderpad in fraction of the legend font size (or prop) adapted from :class:`AnchoredEllipse` """ self._box = AuxTransformBox(transform) self.rectangle = patches.Rectangle((2, 1), width, height, color=color) self._box.add_artist(self.rectangle) AnchoredOffsetbox.__init__(self, loc, pad=pad, borderpad=borderpad, child=self._box, prop=prop, frameon=frameon)
def __init__(self, transform, sizex=0, sizey=0, labelx=None, labely=None, loc=4, pad=0.1, borderpad=0.1, sep=2, prop=None, fontsize='medium', **kwargs): """ Modified, draws a horizontal and/or vertical bar with the size in data coordinate of the give axes. A label will be drawn underneath (center-aligned). Parameters ---------- transform : the coordinate frame (typically axes.transData) sizex, sizey : width of x,y bar, in data units. 0 to omit labelx, labely : labels for x,y bars; None to omit loc : position in containing axes pad, borderpad : padding, in fraction of the legend font size (or prop) sep : separation between labels and bars in points. **kwargs : additional arguments passed to base class constructor Notes ----- Adapted from mpl_toolkits.axes_grid2 """ from matplotlib.lines import Line2D from matplotlib.text import Text from matplotlib.offsetbox import AuxTransformBox bars = AuxTransformBox(transform) inv = transform.inverted() pixelxy = inv.transform((1, 1)) - inv.transform((0, 0)) if sizex: barx = Line2D([sizex, 0], [0, 0], transform=transform, color='k') bars.add_artist(barx) if sizey: bary = Line2D([0, 0], [0, sizey], transform=transform, color='k') bars.add_artist(bary) if sizex and labelx: textx = Text(text=labelx, x=sizex/2.0, y=-5*pixelxy[1], ha='center', va='top', size=fontsize) bars.add_artist(textx) if sizey and labely: texty = Text(text=labely, rotation='vertical', y=sizey/2.0, x=-2*pixelxy[0], va='center', ha='right', size=fontsize) bars.add_artist(texty) AnchoredOffsetbox.__init__(self, loc=loc, pad=pad, borderpad=borderpad, child=bars, prop=prop, frameon=False, **kwargs)
def __init__(self, transform, loc, pad=0.4, borderpad=0.5, prop=None, frameon=True, **kwargs): self.drawing_area = AuxTransformBox(transform) AnchoredOffsetbox.__init__(self, loc, pad=pad, borderpad=borderpad, child=self.drawing_area, prop=prop, frameon=frameon, **kwargs)
def __init__(self, transform, width, height, angle, loc, pad=0.1, borderpad=0.1, prop=None, frameon=False, color="white"): """ Draw an ellipse the size in data coordinate of the give axes. pad, borderpad in fraction of the legend font size (or prop) Copied from https://matplotlib.org/mpl_toolkits/axes_grid/api/anchored_artists_api.html Adapted it a bit (I think) Check how to use original class properly. """ self._box = AuxTransformBox(transform) self.ellipse = patches.Ellipse((0, 0), width, height, angle, color=color) self._box.add_artist(self.ellipse) AnchoredOffsetbox.__init__(self, loc, pad=pad, borderpad=borderpad, child=self._box, prop=prop, frameon=frameon)
def __init__(self, transform, vectors, labels, loc=1, color="black", **kwargs): """ Draw a horizontal bar with the size in data coordinate of the give axes. A label will be drawn underneath (center-aligned). kwargs are passed to AnchoredOffsetbox. """ self.basis_vector = AuxTransformBox(transform) vnorm = np.average([np.linalg.norm(v) for v in vectors]) head_width = 0.3 * vnorm head_length = 0.3 * vnorm position = np.array([0, 0]) if labels is None: labels = [None] * len(vectors) for vector, label in zip(vectors, labels): v2d = np.array(vector[:2]) arrow = FancyArrow(*position, *v2d, color=color, head_width=head_width, head_length=head_length) self.basis_vector.add_artist(arrow) if label is not None: pos = position + v2d * 1.9 label = Text(*pos, label, horizontalalignment='center', verticalalignment='center') self.basis_vector.add_artist(label) AnchoredOffsetbox.__init__(self, loc, child=self.basis_vector)
def __init__(self, transform, size, label, loc, pad=0.1, borderpad=0.1, sep=2, prop=None, frameon=True): """ Draw a horizontal bar with the size in data coordinate of the give axes. A label will be drawn underneath (center-aligned). pad, borderpad in fraction of the legend font size (or prop) sep in points. """ self.size_bar = AuxTransformBox(transform) self.size_bar.add_artist(Rectangle((0,0), size, 0, fc="none")) self.txt_label = TextArea(label, minimumdescent=False) self._box = VPacker(children=[self.size_bar, self.txt_label], align="center", pad=0, sep=sep) AnchoredOffsetbox.__init__(self, loc, pad=pad, borderpad=borderpad, child=self._box, prop=prop, frameon=frameon)
def __init__(self, transform, sizex=0, sizey=0, labelx=None, labely=None, loc=4, pad=0.1, borderpad=0.1, sep=2, prop=None, **kwargs): from matplotlib.patches import Rectangle from matplotlib.offsetbox import AuxTransformBox, VPacker, HPacker, TextArea, DrawingArea bars = AuxTransformBox(transform) if sizex: bars.add_artist(Rectangle((0,0), sizex, 0, fc="none")) if sizey: bars.add_artist(Rectangle((0,0), 0, sizey, fc="none")) if sizex and labelx: bars = VPacker(children=[bars, TextArea(labelx, minimumdescent=False)], align="center", pad=0, sep=sep) if sizey and labely: bars = HPacker(children=[TextArea(labely), bars], align="center", pad=0, sep=sep) AnchoredOffsetbox.__init__(self, loc, pad=pad, borderpad=borderpad, child=bars, prop=prop, frameon=False, **kwargs)
def __init__(self, transform, sizex=0, sizey=0, labelx=None, labely=None, loc=4, pad=0.1, borderpad=0.1, sep=2, prop=None, label_fontsize=label_fontsize, color='k', **kwargs): """ Draw a horizontal and/or vertical bar with the size in data coordinate of the give axes. A label will be drawn underneath (center-aligned). - transform : the coordinate frame (typically axes.transData) - sizex,sizey : width of x,y bar, in data units. 0 to omit - labelx,labely : labels for x,y bars; None to omit - loc : position in containing axes - pad, borderpad : padding, in fraction of the legend font size (or prop) - sep : separation between labels and bars in points. - **kwargs : additional arguments passed to base class constructor """ from matplotlib.patches import Rectangle from matplotlib.offsetbox import AuxTransformBox, VPacker, HPacker, TextArea, DrawingArea bars = AuxTransformBox(transform) if sizex: bars.add_artist(Rectangle((0,0), sizex, 0, fc="none", linewidth=axes_linewidth, color=color)) if sizey: bars.add_artist(Rectangle((0,0), 0, sizey, fc="none", linewidth=axes_linewidth, color=color)) if sizex and labelx: textareax = TextArea(labelx,minimumdescent=False,textprops=dict(size=label_fontsize,color=color)) bars = VPacker(children=[bars, textareax], align="center", pad=0, sep=sep) if sizey and labely: ## VPack a padstr below the rotated labely, else label y goes below the scale bar ## Just adding spaces before labely doesn't work! padstr = '\n '*len(labely) textareafiller = TextArea(padstr,textprops=dict(size=label_fontsize/3.0)) textareay = TextArea(labely,textprops=dict(size=label_fontsize,rotation='vertical',color=color)) ## filler / pad string VPack-ed below labely textareayoffset = VPacker(children=[textareay, textareafiller], align="center", pad=0, sep=sep) ## now HPack this padded labely to the bars bars = HPacker(children=[textareayoffset, bars], align="top", pad=0, sep=sep) AnchoredOffsetbox.__init__(self, loc, pad=pad, borderpad=borderpad, child=bars, prop=prop, frameon=False, **kwargs)
def __init__(self, transform, sizex=0, sizey=0, labelx=None, labely=None, loc=4, pad=0.1, borderpad=0.1, sep=2, linewidth=3, prop=None, fontprops={}, **kwargs): """ Args: - transform : the coordinate frame (typically axes.transData) - sizex,sizey : width of x,y bar, in data units. 0 to omit - labelx,labely : labels for x,y bars; None to omit - loc : position in containing axes, see matplotlib.offsetbox.AnchoredOffsetbox for docs - pad, borderpad : padding, in fraction of the legend font size (or prop) - sep : separation between labels and bars in points. - fontprops: dict specifying text label properties, https://matplotlib.org/users/text_props.html - **kwargs : additional arguments passed to base class constructor """ from matplotlib.patches import Rectangle from matplotlib.lines import Line2D from matplotlib.offsetbox import AuxTransformBox, VPacker, HPacker, TextArea, DrawingArea, OffsetBox fontprops.update({'fontsize': 8}) # need fontprops defaults here, otherwise overwritten on change if in fn defn bars = AuxTransformBox(transform) bars.add_artist(Line2D((0,0,sizex),(sizey,0,0), lw=linewidth, color='k', solid_capstyle='butt', solid_joinstyle='miter')) # Note: this packs the y label and both bars together into a box, then packs the x label below the box, so the # x label is slightly off center of the x bar. This can cause some small alignment problems, but fixing it requires knowledge # of matplotlib offsetboxes, auxtransformboxes, and transforms that I don't have. if sizey and labely: bars = HPacker(children=[TextArea(labely.strip(), textprops=fontprops), bars], align="center", pad=0, sep=sep) if sizex and labelx: bars = VPacker(children=[bars, TextArea(labelx.strip(), minimumdescent=False, textprops=fontprops)], align="center", pad=0, sep=sep) AnchoredOffsetbox.__init__(self, loc, pad=pad, borderpad=borderpad, child=bars, prop=prop, frameon=False, **kwargs)
def __init__(self, transform, width, height, angle, loc, pad=0.1, borderpad=0.1, prop=None, frameon=True, **kwargs): """ Draw an anchored ellipse of a given size. Parameters ---------- transform : `matplotlib.transforms.Transform` The transformation object for the coordinate system in use, i.e., :attr:`matplotlib.axes.Axes.transData`. width, height : float Width and height of the ellipse, given in coordinates of *transform*. angle : float Rotation of the ellipse, in degrees, anti-clockwise. loc : int Location of this size bar. Valid location codes are:: 'upper right' : 1, 'upper left' : 2, 'lower left' : 3, 'lower right' : 4, 'right' : 5, 'center left' : 6, 'center right' : 7, 'lower center' : 8, 'upper center' : 9, 'center' : 10 pad : float, optional Padding around the ellipse, in fraction of the font size. Defaults to 0.1. borderpad : float, optional, default: 0.1 Border padding, in fraction of the font size. frameon : bool, optional, default: True If True, draw a box around the ellipse. prop : `matplotlib.font_manager.FontProperties`, optional Font property used as a reference for paddings. **kwargs Keyworded arguments to pass to :class:`matplotlib.offsetbox.AnchoredOffsetbox`. Attributes ---------- ellipse : `matplotlib.patches.Ellipse` Ellipse patch drawn. """ self._box = AuxTransformBox(transform) self.ellipse = Ellipse((0, 0), width, height, angle) self._box.add_artist(self.ellipse) AnchoredOffsetbox.__init__(self, loc, pad=pad, borderpad=borderpad, child=self._box, prop=prop, frameon=frameon, **kwargs)
def __init__(self, transform, size, label, loc, pad=0.1, borderpad=0.1, sep=2, prop=None, frameon=True, size_vertical=0, color='black', label_top=False, **kwargs): """ Draw a horizontal bar with the size in data coordinate of the give axes. A label will be drawn underneath (center-aligned). Parameters: ----------- transform : matplotlib transformation object size : int or float horizontal length of the size bar, given in data coordinates label : str loc : int pad : int or float, optional in fraction of the legend font size (or prop) borderpad : int or float, optional in fraction of the legend font size (or prop) sep : int or float, optional in points frameon : bool, optional if True, will draw a box around the horizontal bar and label size_vertical : int or float, optional vertical length of the size bar, given in data coordinates color : str, optional color for the size bar and label label_top : bool, optional if true, the label will be over the rectangle Example: -------- >>>> import matplotlib.pyplot as plt >>>> import numpy as np >>>> from mpl_toolkits.axes_grid1.anchored_artists import AnchoredSizeBar >>>> fig, ax = plt.subplots() >>>> ax = imshow(np.random.random((10,10))) >>>> bar = AnchoredSizeBar(ax.transData, 3, '3 units', pad=0.5, loc=4, sep=5, borderpad=0.5, frameon=False, size_vertical=0.5, color='white') >>>> ax.add_artist(bar) >>>> plt.show() """ self.size_bar = AuxTransformBox(transform) self.size_bar.add_artist(Rectangle((0,0), size, size_vertical, fill=True, facecolor=color, edgecolor=color)) self.txt_label = TextArea(label, minimumdescent=False) if label_top: _box_children = [self.txt_label, self.size_bar] else: _box_children = [self.size_bar, self.txt_label] self._box = VPacker(children=_box_children, align="center", pad=0, sep=sep) AnchoredOffsetbox.__init__(self, loc, pad=pad, borderpad=borderpad, child=self._box, prop=prop, frameon=frameon, **kwargs)
def __init__(self, transform, size=0, label=None, horizontal=True, style='dark', loc=4, pad=0.1, borderpad=0.1, sep=2, prop=None, **kwargs): """ Draw a horizontal and/or vertical bar with the size in data coordinate of the give axes. A label will be drawn underneath (center-aligned). - transform : the coordinate frame (typically axes.transData) - sizex,sizey : width / height of the bar, in data units. - label: label for bars; None to omit - horizontal: Whether the bar is horizontal (True) or vertical (False) - style: Whether the bar is dark ('dark') or bright (anything else) - loc : position in containing axes - pad, borderpad : padding, in fraction of the legend font size (or prop) - sep : separation between labels and bars in points. - **kwargs : additional arguments passed to base class constructor """ from matplotlib.offsetbox import AuxTransformBox, VPacker, HPacker, TextArea import matplotlib.patches as mpatches from matplotlib.font_manager import FontProperties fp = FontProperties(size=30.) if style == 'dark': textcol = 'k' else: textcol = 'w' bars = AuxTransformBox(transform) endpt = (size, 0) if horizontal else (0, size) art = mpatches.FancyArrowPatch((0, 0), endpt, color=textcol, arrowstyle="|-|", linewidth=2.5) bars.add_artist(art) packer = VPacker if horizontal else HPacker bars = packer(children=[ bars, TextArea(label, dict(color=textcol, size=20.), minimumdescent=False) ], align="center", pad=0, sep=sep) AnchoredOffsetbox.__init__(self, loc, pad=pad, borderpad=borderpad, child=bars, prop=prop, frameon=False, **kwargs)
def __init__(self, transform, loc, pad=0.4, borderpad=0.5, prop=None, frameon=True, **kwargs): """ An anchored container with transformed coordinates. Artists added to the *drawing_area* are scaled according to the coordinates of the transformation used. The dimensions of this artist will scale to contain the artists added. Parameters ---------- transform : `matplotlib.transforms.Transform` The transformation object for the coordinate system in use, i.e., :attr:`matplotlib.axes.Axes.transData`. loc : int Location of this artist. Valid location codes are:: 'upper right' : 1, 'upper left' : 2, 'lower left' : 3, 'lower right' : 4, 'right' : 5, 'center left' : 6, 'center right' : 7, 'lower center' : 8, 'upper center' : 9, 'center' : 10 pad : int or float, optional Padding around the child objects, in fraction of the font size. Defaults to 0.4. borderpad : int or float, optional Border padding, in fraction of the font size. Defaults to 0.5. prop : `matplotlib.font_manager.FontProperties`, optional Font property used as a reference for paddings. frameon : bool, optional If True, draw a box around this artists. Defaults to True. **kwargs Keyworded arguments to pass to :class:`matplotlib.offsetbox.AnchoredOffsetbox`. Attributes ---------- drawing_area : `matplotlib.offsetbox.AuxTransformBox` A container for artists to display. Examples -------- To display an ellipse in the upper left, with a width of 0.1 and height of 0.4 in data coordinates: >>> box = AnchoredAuxTransformBox(ax.transData, loc='upper left') >>> el = Ellipse((0,0), width=0.1, height=0.4, angle=30) >>> box.drawing_area.add_artist(el) >>> ax.add_artist(box) """ self.drawing_area = AuxTransformBox(transform) AnchoredOffsetbox.__init__(self, loc, pad=pad, borderpad=borderpad, child=self.drawing_area, prop=prop, frameon=frameon, **kwargs)
def __init__(self, transform, loc, pad=0.4, borderpad=0.5, prop=None, frameon=True, **kwargs): """ An anchored container with transformed coordinates. Artists added to the *drawing_area* are scaled according to the coordinates of the transformation used. The dimensions of this artist will scale to contain the artists added. Parameters ---------- transform : `matplotlib.transforms.Transform` The transformation object for the coordinate system in use, i.e., :attr:`matplotlib.axes.Axes.transData`. loc : int Location of this artist. Valid location codes are:: 'upper right' : 1, 'upper left' : 2, 'lower left' : 3, 'lower right' : 4, 'right' : 5, 'center left' : 6, 'center right' : 7, 'lower center' : 8, 'upper center' : 9, 'center' : 10 pad : float, optional, default: 0.4 Padding around the child objects, in fraction of the font size. borderpad : float, optional, default: 0.5 Border padding, in fraction of the font size. prop : `matplotlib.font_manager.FontProperties`, optional Font property used as a reference for paddings. frameon : bool, optional, default: True If True, draw a box around this artists. **kwargs Keyworded arguments to pass to :class:`matplotlib.offsetbox.AnchoredOffsetbox`. Attributes ---------- drawing_area : `matplotlib.offsetbox.AuxTransformBox` A container for artists to display. Examples -------- To display an ellipse in the upper left, with a width of 0.1 and height of 0.4 in data coordinates: >>> box = AnchoredAuxTransformBox(ax.transData, loc='upper left') >>> el = Ellipse((0, 0), width=0.1, height=0.4, angle=30) >>> box.drawing_area.add_artist(el) >>> ax.add_artist(box) """ self.drawing_area = AuxTransformBox(transform) AnchoredOffsetbox.__init__(self, loc, pad=pad, borderpad=borderpad, child=self.drawing_area, prop=prop, frameon=frameon, **kwargs)
def __init__(self, transform, sizex=0, sizey=0, labelx=None, labely=None, loc=4, pad=0.1, borderpad=0.1, sep=2, prop=None, barcolor="black", barwidth=0.4, **kwargs): """ Draw a horizontal and/or vertical bar with the size in data coordinate of the give axes. A label will be drawn underneath (center-aligned). - transform : the coordinate frame (typically axes.transData) - sizex,sizey : width of x,y bar, in data units. 0 to omit - labelx,labely : labels for x,y bars; None to omit - loc : position in containing axes - pad, borderpad : padding, in fraction of the legend font size (or prop) - sep : separation between labels and bars in points. - **kwargs : additional arguments passed to base class constructor """ bars = AuxTransformBox(transform) if sizex: arr = FancyArrowPatch( (0, 0), (sizex, 0), shrinkA=0, shrinkB=0, ec=barcolor, lw=barwidth, fc="none", arrowstyle="|-|", ) arr.set_capstyle("butt") bars.add_artist(arr) if sizey: bars.add_artist( Rectangle((0, 0), 0, sizey, ec=barcolor, lw=barwidth, fc="none")) if sizex and labelx: self.xlabel = TextArea(labelx, minimumdescent=False) bars = VPacker(children=[bars, self.xlabel], align="center", pad=0, sep=sep) if sizey and labely: self.ylabel = TextArea(labely) bars = HPacker(children=[self.ylabel, bars], align="center", pad=0, sep=sep) AnchoredOffsetbox.__init__(self, loc, pad=pad, borderpad=borderpad, child=bars, prop=prop, frameon=False, **kwargs)
def __init__(self, transform, size, label, loc, pad=0.1, borderpad=0.1, sep=2, frameon=True, size_vertical=0, color='black', label_top=False, fontproperties=None, **kwargs): """ Draw a horizontal bar with the size in data coordinate of the given axes. A label will be drawn underneath (center-aligned). Parameters: ----------- transform : matplotlib transformation object size : int or float horizontal length of the size bar, given in data coordinates label : str loc : int pad : int or float, optional in fraction of the legend font size (or prop) borderpad : int or float, optional in fraction of the legend font size (or prop) sep : int or float, optional in points frameon : bool, optional if True, will draw a box around the horizontal bar and label size_vertical : int or float, optional vertical length of the size bar, given in data coordinates color : str, optional color for the size bar and label label_top : bool, optional if True, the label will be over the rectangle fontproperties: a matplotlib.font_manager.FontProperties instance, optional sets the font properties for the label text Returns: -------- AnchoredSizeBar object Example: -------- >>> import matplotlib.pyplot as plt >>> import numpy as np >>> from mpl_toolkits.axes_grid1.anchored_artists import AnchoredSizeBar >>> fig, ax = plt.subplots() >>> ax.imshow(np.random.random((10,10))) >>> bar = AnchoredSizeBar(ax.transData, 3, '3 units', 4) >>> ax.add_artist(bar) >>> fig.show() Using all the optional parameters >>> import matplotlib.font_manager as fm >>> fontprops = fm.FontProperties(size=14, family='monospace') >>> bar = AnchoredSizeBar(ax.transData, 3, '3 units', 4, pad=0.5, sep=5, borderpad=0.5, frameon=False, size_vertical=0.5, color='white', fontproperties=fontprops) # noqa """ self.size_bar = AuxTransformBox(transform) self.size_bar.add_artist(Rectangle((0, 0), size, size_vertical, fill=True, facecolor=color, edgecolor=color)) # if fontproperties is None, but `prop` is not, assume that # prop should be used to set the font properties. This is # questionable behavior if fontproperties is None and 'prop' in kwargs: fontproperties = kwargs.pop('prop') if fontproperties is None: textprops = {'color': color} else: textprops = {'color': color, 'fontproperties': fontproperties} self.txt_label = TextArea( label, minimumdescent=False, textprops=textprops) if label_top: _box_children = [self.txt_label, self.size_bar] else: _box_children = [self.size_bar, self.txt_label] self._box = VPacker(children=_box_children, align="center", pad=0, sep=sep) AnchoredOffsetbox.__init__(self, loc, pad=pad, borderpad=borderpad, child=self._box, prop=fontproperties, frameon=frameon, **kwargs)
def __init__(self, transform, width, height, angle, loc, pad=0.1, borderpad=0.1, prop=None, frameon=True, **kwargs): """ Draw an anchored ellipse of a given size. Parameters ---------- transform : `matplotlib.transforms.Transform` The transformation object for the coordinate system in use, i.e., :attr:`matplotlib.axes.Axes.transData`. width, height : int or float Width and height of the ellipse, given in coordinates of *transform*. angle : int or float Rotation of the ellipse, in degrees, anti-clockwise. loc : int Location of this size bar. Valid location codes are:: 'upper right' : 1, 'upper left' : 2, 'lower left' : 3, 'lower right' : 4, 'right' : 5, 'center left' : 6, 'center right' : 7, 'lower center' : 8, 'upper center' : 9, 'center' : 10 pad : int or float, optional Padding around the ellipse, in fraction of the font size. Defaults to 0.1. borderpad : int or float, optional Border padding, in fraction of the font size. Defaults to 0.1. frameon : bool, optional If True, draw a box around the ellipse. Defaults to True. prop : `matplotlib.font_manager.FontProperties`, optional Font property used as a reference for paddings. **kwargs Keyworded arguments to pass to :class:`matplotlib.offsetbox.AnchoredOffsetbox`. Attributes ---------- ellipse : `matplotlib.patches.Ellipse` Ellipse patch drawn. """ self._box = AuxTransformBox(transform) self.ellipse = Ellipse((0, 0), width, height, angle) self._box.add_artist(self.ellipse) AnchoredOffsetbox.__init__(self, loc, pad=pad, borderpad=borderpad, child=self._box, prop=prop, frameon=frameon, **kwargs)
def __init__(self, transform, size, label, loc, pad=0.1, borderpad=0.1, sep=2, frameon=True, size_vertical=0, color='black', label_top=False, fontproperties=None, **kwargs): """ Draw a horizontal bar with the size in data coordinate of the given axes. A label will be drawn underneath (center-aligned). Parameters: ----------- transform : matplotlib transformation object size : int or float horizontal length of the size bar, given in data coordinates label : str loc : int pad : int or float, optional in fraction of the legend font size (or prop) borderpad : int or float, optional in fraction of the legend font size (or prop) sep : int or float, optional in points frameon : bool, optional if True, will draw a box around the horizontal bar and label size_vertical : int or float, optional vertical length of the size bar, given in data coordinates color : str, optional color for the size bar and label label_top : bool, optional if True, the label will be over the rectangle fontproperties: a matplotlib.font_manager.FontProperties instance, optional sets the font properties for the label text Returns: -------- AnchoredSizeBar object Example: -------- >>> import matplotlib.pyplot as plt >>> import numpy as np >>> from mpl_toolkits.axes_grid1.anchored_artists import AnchoredSizeBar >>> fig, ax = plt.subplots() >>> ax.imshow(np.random.random((10,10))) >>> bar = AnchoredSizeBar(ax.transData, 3, '3 units', 4) >>> ax.add_artist(bar) >>> fig.show() Using all the optional parameters >>> import matplotlib.font_manager as fm >>> fontprops = fm.FontProperties(size=14, family='monospace') >>> bar = AnchoredSizeBar(ax.transData, 3, '3 units', 4, pad=0.5, sep=5, borderpad=0.5, frameon=False, size_vertical=0.5, color='white', fontproperties=fontprops) """ self.size_bar = AuxTransformBox(transform) self.size_bar.add_artist( Rectangle((0, 0), size, size_vertical, fill=True, facecolor=color, edgecolor=color)) # if fontproperties is None, but `prop` is not, assume that # prop should be used to set the font properties. This is # questionable behavior if fontproperties is None and 'prop' in kwargs: fontproperties = kwargs.pop('prop') if fontproperties is None: textprops = {'color': color} else: textprops = {'color': color, 'fontproperties': fontproperties} self.txt_label = TextArea(label, minimumdescent=False, textprops=textprops) if label_top: _box_children = [self.txt_label, self.size_bar] else: _box_children = [self.size_bar, self.txt_label] self._box = VPacker(children=_box_children, align="center", pad=0, sep=sep) AnchoredOffsetbox.__init__(self, loc, pad=pad, borderpad=borderpad, child=self._box, prop=fontproperties, frameon=frameon, **kwargs)
def __init__(self, ax, ori, loc=4, arrow_fraction=0.15, txt1="E", txt2="N", pad=0.3, borderpad=0.5, prop=None, frameon=False): self._ax = ax self.ori = ori self._box = AuxTransformBox(ax.transData) self.arrow_fraction = arrow_fraction path_effects = [PathEffects.withStroke(linewidth=3, foreground="w")] kwargs = dict(mutation_scale=14, shrinkA=0, shrinkB=7) self.arrow1 = FancyArrowPatch(posA=(0, 0), posB=(1, 1), arrowstyle="-|>", arrow_transmuter=None, connectionstyle="arc3", connector=None, color="k", path_effects=path_effects, **kwargs) self.arrow2 = FancyArrowPatch(posA=(0, 0), posB=(1, 1), arrowstyle="-|>", arrow_transmuter=None, connectionstyle="arc3", connector=None, color="k", path_effects=path_effects, **kwargs) self.txt1 = Text(1, 1, txt1, rotation=0, rotation_mode="anchor", path_effects=path_effects, va="center", ha="center") self.txt2 = Text(2, 2, txt2, rotation=0, rotation_mode="anchor", path_effects=path_effects, va="center", ha="center") self._box.add_artist(self.arrow1) self._box.add_artist(self.arrow2) self._box.add_artist(self.txt1) self._box.add_artist(self.txt2) AnchoredOffsetbox.__init__(self, loc, pad=pad, borderpad=borderpad, child=self._box, prop=prop, frameon=frameon)
def __init__(self, ax, transform, width, height, zorder, xlabel, fc, ylabels=None, loc=4, fontsize=5, pad=0.1, borderpad=0.1, sep=2, prop=None, add_ruler=False, ruler_unit='Km', ruler_unit_fontsize=7, ruler_fontweight='bold', tick_fontweight='light', **kwargs): """ Draw a horizontal and/or vertical bar with the size in data coordinate of the give axes. A label will be drawn underneath (center-aligned). - transform : the coordinate frame (typically axes.transData) - sizex,sizey : width of x,y bar, in data units. 0 to omit - labelx,labely : labels for x,y bars; None to omit - loc : position in containing axes - pad, borderpad : padding, in fraction of the legend font size (or prop) - sep : separation between labels and bars in points. - **kwargs : additional arguments passed to base class constructor """ if ruler_unit_fontsize is None: ruler_unit_fontsize = fontsize * 1.5 Rect = Rectangle( (0, 0), width, height, fc=fc, edgecolor='k', zorder=zorder, ) ATB = AuxTransformBox(transform) ATB.add_artist(Rect) Txt_xlabel = TextArea(xlabel, textprops=dict(fontsize=fontsize, fontweight=tick_fontweight), minimumdescent=True) # vertically packing a single stripe with respective label child = VPacker(children=[Txt_xlabel, ATB], align="right", pad=5, sep=0) if add_ruler: Text = TextArea(ruler_unit, textprops=dict(fontsize=ruler_unit_fontsize, fontweight=ruler_fontweight)) child = VPacker(children=[child, Text], align="center", pad=5, sep=0) else: Text = TextArea('', textprops=dict(fontsize=ruler_unit_fontsize)) child = VPacker(children=[child, Text], align="right", pad=5, sep=0) # horizontally packing all child packs in a single offsetBox AnchoredOffsetbox.__init__(self, loc='center left', borderpad=borderpad, child=child, prop=prop, frameon=False, **kwargs)
def __init__(self, ax, transSky2Pix, loc, arrow_fraction=0.15, txt1="E", txt2="N", delta_a1=0, delta_a2=0, pad=0.1, borderpad=0.5, prop=None, frameon=False, color=None): """ Draw an arrows pointing the directions of E & N arrow_fraction : length of the arrow as a fraction of axes size pad, borderpad in fraction of the legend font size (or prop) """ self._ax = ax self._transSky2Pix = transSky2Pix self._box = AuxTransformBox(ax.transData) self.delta_a1, self.delta_a2 = delta_a1, delta_a2 self.arrow_fraction = arrow_fraction kwargs = dict(mutation_scale=11, shrinkA=0, shrinkB=5) self.arrow1 = FancyArrowPatch(posA=(0, 0), posB=(1, 1), arrowstyle="->", arrow_transmuter=None, connectionstyle="arc3", connector=None, color=color, **kwargs) self.arrow2 = FancyArrowPatch(posA=(0, 0), posB=(1, 1), arrowstyle="->", arrow_transmuter=None, connectionstyle="arc3", connector=None, color=color, **kwargs) x1t, y1t, x2t, y2t = 1, 1, 1, 1 self.txt1 = Text(x1t, y1t, txt1, rotation=0, rotation_mode="anchor", va="center", ha="right", color=color, fontproperties=prop) self.txt2 = Text(x2t, y2t, txt2, rotation=0, rotation_mode="anchor", va="bottom", ha="center", color=color, fontproperties=prop) self._box.add_artist(self.arrow1) self._box.add_artist(self.arrow2) self._box.add_artist(self.txt1) self._box.add_artist(self.txt2) AnchoredOffsetbox.__init__(self, loc, pad=pad, borderpad=borderpad, child=self._box, prop=prop, frameon=frameon)
def __init__(self, transform, size, label, loc, pad=0.1, borderpad=0.1, sep=2, prop=None, frameon=True, size_vertical=0, color='black', label_top=False, **kwargs): """ Draw a horizontal bar with the size in data coordinate of the give axes. A label will be drawn underneath (center-aligned). Parameters: ----------- transform : matplotlib transformation object size : int or float horizontal length of the size bar, given in data coordinates label : str loc : int pad : int or float, optional in fraction of the legend font size (or prop) borderpad : int or float, optional in fraction of the legend font size (or prop) sep : int or float, optional in points frameon : bool, optional if True, will draw a box around the horizontal bar and label size_vertical : int or float, optional vertical length of the size bar, given in data coordinates color : str, optional color for the size bar and label label_top : bool, optional if true, the label will be over the rectangle Example: -------- >>>> import matplotlib.pyplot as plt >>>> import numpy as np >>>> from mpl_toolkits.axes_grid1.anchored_artists import AnchoredSizeBar >>>> fig, ax = plt.subplots() >>>> ax = imshow(np.random.random((10,10))) >>>> bar = AnchoredSizeBar(ax.transData, 3, '3 units', pad=0.5, loc=4, sep=5, borderpad=0.5, frameon=False, size_vertical=0.5, color='white') >>>> ax.add_artist(bar) >>>> plt.show() """ self.size_bar = AuxTransformBox(transform) self.size_bar.add_artist( Rectangle((0, 0), size, size_vertical, fill=True, facecolor=color, edgecolor=color)) self.txt_label = TextArea(label, minimumdescent=False) if label_top: _box_children = [self.txt_label, self.size_bar] else: _box_children = [self.size_bar, self.txt_label] self._box = VPacker(children=_box_children, align="center", pad=0, sep=sep) AnchoredOffsetbox.__init__(self, loc, pad=pad, borderpad=borderpad, child=self._box, prop=prop, frameon=frameon, **kwargs)
def __init__(self, transform, size, label, loc, pad=0.1, borderpad=0.1, sep=2, frameon=True, size_vertical=0, color='black', label_top=False, fontproperties=None, fill_bar=None, **kwargs): """ Draw a horizontal scale bar with a center-aligned label underneath. Parameters ---------- transform : `matplotlib.transforms.Transform` The transformation object for the coordinate system in use, i.e., :attr:`matplotlib.axes.Axes.transData`. size : float Horizontal length of the size bar, given in coordinates of *transform*. label : str Label to display. loc : int Location of this size bar. Valid location codes are:: 'upper right' : 1, 'upper left' : 2, 'lower left' : 3, 'lower right' : 4, 'right' : 5, 'center left' : 6, 'center right' : 7, 'lower center' : 8, 'upper center' : 9, 'center' : 10 pad : float, optional, default: 0.1 Padding around the label and size bar, in fraction of the font size. borderpad : float, optional, default: 0.1 Border padding, in fraction of the font size. sep : float, optional, default: 2 Separation between the label and the size bar, in points. frameon : bool, optional, default: True If True, draw a box around the horizontal bar and label. size_vertical : float, optional, default: 0 Vertical length of the size bar, given in coordinates of *transform*. color : str, optional, default: 'black' Color for the size bar and label. label_top : bool, optional, default: False If True, the label will be over the size bar. fontproperties : `matplotlib.font_manager.FontProperties`, optional Font properties for the label text. fill_bar : bool, optional If True and if size_vertical is nonzero, the size bar will be filled in with the color specified by the size bar. Defaults to True if `size_vertical` is greater than zero and False otherwise. **kwargs Keyworded arguments to pass to :class:`matplotlib.offsetbox.AnchoredOffsetbox`. Attributes ---------- size_bar : `matplotlib.offsetbox.AuxTransformBox` Container for the size bar. txt_label : `matplotlib.offsetbox.TextArea` Container for the label of the size bar. Notes ----- If *prop* is passed as a keyworded argument, but *fontproperties* is not, then *prop* is be assumed to be the intended *fontproperties*. Using both *prop* and *fontproperties* is not supported. Examples -------- >>> import matplotlib.pyplot as plt >>> import numpy as np >>> from mpl_toolkits.axes_grid1.anchored_artists import ( ... AnchoredSizeBar) >>> fig, ax = plt.subplots() >>> ax.imshow(np.random.random((10, 10))) >>> bar = AnchoredSizeBar(ax.transData, 3, '3 data units', 4) >>> ax.add_artist(bar) >>> fig.show() Using all the optional parameters >>> import matplotlib.font_manager as fm >>> fontprops = fm.FontProperties(size=14, family='monospace') >>> bar = AnchoredSizeBar(ax.transData, 3, '3 units', 4, pad=0.5, ... sep=5, borderpad=0.5, frameon=False, ... size_vertical=0.5, color='white', ... fontproperties=fontprops) """ if fill_bar is None: fill_bar = size_vertical > 0 self.size_bar = AuxTransformBox(transform) self.size_bar.add_artist( Rectangle((0, 0), size, size_vertical, fill=fill_bar, facecolor=color, edgecolor=color)) if fontproperties is None and 'prop' in kwargs: fontproperties = kwargs.pop('prop') if fontproperties is None: textprops = {'color': color} else: textprops = {'color': color, 'fontproperties': fontproperties} self.txt_label = TextArea(label, minimumdescent=False, textprops=textprops) if label_top: _box_children = [self.txt_label, self.size_bar] else: _box_children = [self.size_bar, self.txt_label] self._box = VPacker(children=_box_children, align="center", pad=0, sep=sep) AnchoredOffsetbox.__init__(self, loc, pad=pad, borderpad=borderpad, child=self._box, prop=fontproperties, frameon=frameon, **kwargs)
def __init__(self, transform, size, label, loc, pad=0.1, borderpad=0.1, sep=2, frameon=True, size_vertical=0, color='black', label_top=False, fontproperties=None, fill_bar=None, **kwargs): """ Draw a horizontal scale bar with a center-aligned label underneath. Parameters ---------- transform : `matplotlib.transforms.Transform` The transformation object for the coordinate system in use, i.e., :attr:`matplotlib.axes.Axes.transData`. size : int or float Horizontal length of the size bar, given in coordinates of *transform*. label : str Label to display. loc : int Location of this size bar. Valid location codes are:: 'upper right' : 1, 'upper left' : 2, 'lower left' : 3, 'lower right' : 4, 'right' : 5, 'center left' : 6, 'center right' : 7, 'lower center' : 8, 'upper center' : 9, 'center' : 10 pad : int or float, optional Padding around the label and size bar, in fraction of the font size. Defaults to 0.1. borderpad : int or float, optional Border padding, in fraction of the font size. Defaults to 0.1. sep : int or float, optional Separation between the label and the size bar, in points. Defaults to 2. frameon : bool, optional If True, draw a box around the horizontal bar and label. Defaults to True. size_vertical : int or float, optional Vertical length of the size bar, given in coordinates of *transform*. Defaults to 0. color : str, optional Color for the size bar and label. Defaults to black. label_top : bool, optional If True, the label will be over the size bar. Defaults to False. fontproperties : `matplotlib.font_manager.FontProperties`, optional Font properties for the label text. fill_bar : bool, optional If True and if size_vertical is nonzero, the size bar will be filled in with the color specified by the size bar. Defaults to True if `size_vertical` is greater than zero and False otherwise. **kwargs Keyworded arguments to pass to :class:`matplotlib.offsetbox.AnchoredOffsetbox`. Attributes ---------- size_bar : `matplotlib.offsetbox.AuxTransformBox` Container for the size bar. txt_label : `matplotlib.offsetbox.TextArea` Container for the label of the size bar. Notes ----- If *prop* is passed as a keyworded argument, but *fontproperties* is not, then *prop* is be assumed to be the intended *fontproperties*. Using both *prop* and *fontproperties* is not supported. Examples -------- >>> import matplotlib.pyplot as plt >>> import numpy as np >>> from mpl_toolkits.axes_grid1.anchored_artists import ( ... AnchoredSizeBar) >>> fig, ax = plt.subplots() >>> ax.imshow(np.random.random((10,10))) >>> bar = AnchoredSizeBar(ax.transData, 3, '3 data units', 4) >>> ax.add_artist(bar) >>> fig.show() Using all the optional parameters >>> import matplotlib.font_manager as fm >>> fontprops = fm.FontProperties(size=14, family='monospace') >>> bar = AnchoredSizeBar(ax.transData, 3, '3 units', 4, pad=0.5, ... sep=5, borderpad=0.5, frameon=False, ... size_vertical=0.5, color='white', ... fontproperties=fontprops) """ if fill_bar is None: fill_bar = size_vertical > 0 self.size_bar = AuxTransformBox(transform) self.size_bar.add_artist(Rectangle((0, 0), size, size_vertical, fill=fill_bar, facecolor=color, edgecolor=color)) if fontproperties is None and 'prop' in kwargs: fontproperties = kwargs.pop('prop') if fontproperties is None: textprops = {'color': color} else: textprops = {'color': color, 'fontproperties': fontproperties} self.txt_label = TextArea( label, minimumdescent=False, textprops=textprops) if label_top: _box_children = [self.txt_label, self.size_bar] else: _box_children = [self.size_bar, self.txt_label] self._box = VPacker(children=_box_children, align="center", pad=0, sep=sep) AnchoredOffsetbox.__init__(self, loc, pad=pad, borderpad=borderpad, child=self._box, prop=fontproperties, frameon=frameon, **kwargs)
def __init__(self, transform, label_x, label_y, length=0.15, fontsize=0.08, loc=2, angle=0, aspect_ratio=1, pad=0.4, borderpad=0.4, frameon=False, color='w', alpha=1, sep_x=0.01, sep_y=0, fontproperties=None, back_length=0.15, head_width=10, head_length=15, tail_width=2, text_props=None, arrow_props=None, **kwargs): """ Draw two perpendicular arrows to indicate directions. Parameters ---------- transform : `matplotlib.transforms.Transform` The transformation object for the coordinate system in use, i.e., :attr:`matplotlib.axes.Axes.transAxes`. label_x, label_y : str Label text for the x and y arrows length : float, optional, default: 0.15 Length of the arrow, given in coordinates of *transform*. fontsize : float, optional, default: 0.08 Size of label strings, given in coordinates of *transform*. loc : int, optional, default: 2 Location of the direction arrows. Valid location codes are:: 'upper right' : 1, 'upper left' : 2, 'lower left' : 3, 'lower right' : 4, 'right' : 5, 'center left' : 6, 'center right' : 7, 'lower center' : 8, 'upper center' : 9, 'center' : 10 angle : float, optional, default: 0 The angle of the arrows in degrees. aspect_ratio : float, optional, default: 1 The ratio of the length of arrow_x and arrow_y. Negative numbers can be used to change the direction. pad : float, optional, default: 0.4 Padding around the labels and arrows, in fraction of the font size. borderpad : float, optional, default: 0.4 Border padding, in fraction of the font size. frameon : bool, optional, default: False If True, draw a box around the arrows and labels. color : str, optional, default: 'white' Color for the arrows and labels. alpha : float, optional, default: 1 Alpha values of the arrows and labels sep_x, sep_y : float, optional, default: 0.01 and 0 respectively Separation between the arrows and labels in coordinates of *transform*. fontproperties : `matplotlib.font_manager.FontProperties`, optional Font properties for the label text. back_length : float, optional, default: 0.15 Fraction of the arrow behind the arrow crossing. head_width : float, optional, default: 10 Width of arrow head, sent to ArrowStyle. head_length : float, optional, default: 15 Length of arrow head, sent to ArrowStyle. tail_width : float, optional, default: 2 Width of arrow tail, sent to ArrowStyle. text_props, arrow_props : dict Properties of the text and arrows, passed to `.textpath.TextPath` and `.patches.FancyArrowPatch`. **kwargs Keyworded arguments to pass to :class:`matplotlib.offsetbox.AnchoredOffsetbox`. Attributes ---------- arrow_x, arrow_y : `matplotlib.patches.FancyArrowPatch` Arrow x and y text_path_x, text_path_y : `matplotlib.textpath.TextPath` Path for arrow labels p_x, p_y : `matplotlib.patches.PathPatch` Patch for arrow labels box : `matplotlib.offsetbox.AuxTransformBox` Container for the arrows and labels. Notes ----- If *prop* is passed as a keyword argument, but *fontproperties* is not, then *prop* is be assumed to be the intended *fontproperties*. Using both *prop* and *fontproperties* is not supported. Examples -------- >>> import matplotlib.pyplot as plt >>> import numpy as np >>> from mpl_toolkits.axes_grid1.anchored_artists import ( ... AnchoredDirectionArrows) >>> fig, ax = plt.subplots() >>> ax.imshow(np.random.random((10, 10))) >>> arrows = AnchoredDirectionArrows(ax.transAxes, '111', '110') >>> ax.add_artist(arrows) >>> fig.show() Using several of the optional parameters, creating downward pointing arrow and high contrast text labels. >>> import matplotlib.font_manager as fm >>> fontprops = fm.FontProperties(family='monospace') >>> arrows = AnchoredDirectionArrows(ax.transAxes, 'East', 'South', ... loc='lower left', color='k', ... aspect_ratio=-1, sep_x=0.02, ... sep_y=-0.01, ... text_props={'ec':'w', 'fc':'k'}, ... fontproperties=fontprops) """ if arrow_props is None: arrow_props = {} if text_props is None: text_props = {} arrowstyle = ArrowStyle("Simple", head_width=head_width, head_length=head_length, tail_width=tail_width) if fontproperties is None and 'prop' in kwargs: fontproperties = kwargs.pop('prop') if 'color' not in arrow_props: arrow_props['color'] = color if 'alpha' not in arrow_props: arrow_props['alpha'] = alpha if 'color' not in text_props: text_props['color'] = color if 'alpha' not in text_props: text_props['alpha'] = alpha t_start = transform t_end = t_start + transforms.Affine2D().rotate_deg(angle) self.box = AuxTransformBox(t_end) length_x = length length_y = length * aspect_ratio self.arrow_x = FancyArrowPatch((0, back_length * length_y), (length_x, back_length * length_y), arrowstyle=arrowstyle, shrinkA=0.0, shrinkB=0.0, **arrow_props) self.arrow_y = FancyArrowPatch((back_length * length_x, 0), (back_length * length_x, length_y), arrowstyle=arrowstyle, shrinkA=0.0, shrinkB=0.0, **arrow_props) self.box.add_artist(self.arrow_x) self.box.add_artist(self.arrow_y) text_path_x = TextPath( (length_x + sep_x, back_length * length_y + sep_y), label_x, size=fontsize, prop=fontproperties) self.p_x = PathPatch(text_path_x, transform=t_start, **text_props) self.box.add_artist(self.p_x) text_path_y = TextPath((length_x * back_length + sep_x, length_y * (1 - back_length) + sep_y), label_y, size=fontsize, prop=fontproperties) self.p_y = PathPatch(text_path_y, **text_props) self.box.add_artist(self.p_y) AnchoredOffsetbox.__init__(self, loc, pad=pad, borderpad=borderpad, child=self.box, frameon=frameon, **kwargs)
def __init__(self, transform, label_x, label_y, length=0.15, fontsize=0.08, loc=2, angle=0, aspect_ratio=1, pad=0.4, borderpad=0.4, frameon=False, color='w', alpha=1, sep_x=0.01, sep_y=0, fontproperties=None, back_length=0.15, head_width=10, head_length=15, tail_width=2, text_props=None, arrow_props=None, **kwargs): """ Draw two perpendicular arrows to indicate directions. Parameters ---------- transform : `matplotlib.transforms.Transform` The transformation object for the coordinate system in use, i.e., :attr:`matplotlib.axes.Axes.transAxes`. label_x, label_y : string Label text for the x and y arrows length : int or float, optional Length of the arrow, given in coordinates of *transform*. Defaults to 0.15. fontsize : int, optional Size of label strings, given in coordinates of *transform*. Defaults to 0.08. loc : int, optional Location of the direction arrows. Valid location codes are:: 'upper right' : 1, 'upper left' : 2, 'lower left' : 3, 'lower right' : 4, 'right' : 5, 'center left' : 6, 'center right' : 7, 'lower center' : 8, 'upper center' : 9, 'center' : 10 Defaults to 2. angle : int or float, optional The angle of the arrows in degrees. Defaults to 0. aspect_ratio : int or float, optional The ratio of the length of arrow_x and arrow_y. Negative numbers can be used to change the direction. Defaults to 1. pad : int or float, optional Padding around the labels and arrows, in fraction of the font size. Defaults to 0.4. borderpad : int or float, optional Border padding, in fraction of the font size. Defaults to 0.4. frameon : bool, optional If True, draw a box around the arrows and labels. Defaults to False. color : str, optional Color for the arrows and labels. Defaults to white. alpha : int or float, optional Alpha values of the arrows and labels Defaults to 1. sep_x, sep_y : int or float, optional Separation between the arrows and labels in coordinates of *transform*. Defaults to 0.01 and 0. fontproperties : `matplotlib.font_manager.FontProperties`, optional Font properties for the label text. back_length : float, optional Fraction of the arrow behind the arrow crossing. Defaults to 0.15. head_width : int or float, optional Width of arrow head, sent to ArrowStyle. Defaults to 10. head_length : int or float, optional Length of arrow head, sent to ArrowStyle. Defaults to 15. tail_width : int or float, optional Width of arrow tail, sent to ArrowStyle. Defaults to 2. text_props, arrow_props : dict Properties of the text and arrows, passed to :class:`matplotlib.text.TextPath` and `matplotlib.patches.FancyArrowPatch` **kwargs Keyworded arguments to pass to :class:`matplotlib.offsetbox.AnchoredOffsetbox`. Attributes ---------- arrow_x, arrow_y : `matplotlib.patches.FancyArrowPatch` Arrow x and y text_path_x, text_path_y : `matplotlib.text.TextPath` Path for arrow labels p_x, p_y : `matplotlib.patches.PathPatch` Patch for arrow labels box : `matplotlib.offsetbox.AuxTransformBox` Container for the arrows and labels. Notes ----- If *prop* is passed as a keyword argument, but *fontproperties* is not, then *prop* is be assumed to be the intended *fontproperties*. Using both *prop* and *fontproperties* is not supported. Examples -------- >>> import matplotlib.pyplot as plt >>> import numpy as np >>> from mpl_toolkits.axes_grid1.anchored_artists import ( ... AnchoredDirectionArrows) >>> fig, ax = plt.subplots() >>> ax.imshow(np.random.random((10,10))) >>> arrows = AnchoredDirectionArrows(ax.transAxes, '111', '110') >>> ax.add_artist(arrows) >>> fig.show() Using several of the optional parameters, creating downward pointing arrow and high contrast text labels. >>> import matplotlib.font_manager as fm >>> fontprops = fm.FontProperties(family='monospace') >>> arrows = AnchoredDirectionArrows(ax.transAxes, 'East', 'South', ... loc='lower left', color='k', ... aspect_ratio=-1, sep_x=0.02, ... sep_y=-0.01, ... text_props={'ec':'w', 'fc':'k'}, ... fontproperties=fontprops) """ if arrow_props is None: arrow_props = {} if text_props is None: text_props = {} arrowstyle = ArrowStyle("Simple", head_width=head_width, head_length=head_length, tail_width=tail_width) if fontproperties is None and 'prop' in kwargs: fontproperties = kwargs.pop('prop') if 'color' not in arrow_props: arrow_props['color'] = color if 'alpha' not in arrow_props: arrow_props['alpha'] = alpha if 'color' not in text_props: text_props['color'] = color if 'alpha' not in text_props: text_props['alpha'] = alpha t_start = transform t_end = t_start + transforms.Affine2D().rotate_deg(angle) self.box = AuxTransformBox(t_end) length_x = length length_y = length*aspect_ratio self.arrow_x = FancyArrowPatch( (0, back_length*length_y), (length_x, back_length*length_y), arrowstyle=arrowstyle, shrinkA=0.0, shrinkB=0.0, **arrow_props) self.arrow_y = FancyArrowPatch( (back_length*length_x, 0), (back_length*length_x, length_y), arrowstyle=arrowstyle, shrinkA=0.0, shrinkB=0.0, **arrow_props) self.box.add_artist(self.arrow_x) self.box.add_artist(self.arrow_y) text_path_x = TextPath(( length_x+sep_x, back_length*length_y+sep_y), label_x, size=fontsize, prop=fontproperties) self.p_x = PathPatch(text_path_x, transform=t_start, **text_props) self.box.add_artist(self.p_x) text_path_y = TextPath(( length_x*back_length+sep_x, length_y*(1-back_length)+sep_y), label_y, size=fontsize, prop=fontproperties) self.p_y = PathPatch(text_path_y, **text_props) self.box.add_artist(self.p_y) AnchoredOffsetbox.__init__(self, loc, pad=pad, borderpad=borderpad, child=self.box, frameon=frameon, **kwargs)
def __init__(self, transform, *, sizex=0, sizey=0, labelx=None, labely=None, loc=4, pad=0.5, borderpad=0.1, sep=2, prop=None, ec='k', fc='k', fontsize=None, lw=1.5, capstyle='projecting', xfirst=True, **kwargs): """ Draw a horizontal and/or vertical bar with the size in data coordinate of the give axes. A label will be drawn underneath (center-aligned). - transform : the coordinate frame (typically axes.transData) - sizex,sizey : width of x,y bar, in data units. 0 to omit - labelx,labely : labels for x,y bars; None to omit - loc : (int) position in containing axes 'upper right' : 1, 'upper left' : 2, 'lower left' : 3, 'lower right' : 4, default 'right' : 5, 'center left' : 6, 'center right' : 7, 'lower center' : 8, 'upper center' : 9, 'center' : 10, - pad, borderpad : padding, in fraction of the legend font size - sep : separation between labels and bars in points. - ec : edgecolor of scalebar - lw : linewidth of scalebar - fontsize : font size of labels - fc : font color / face color of labels - capstyle : capstyle of bars ['round', 'butt', 'projecting'] - **kwargs : additional arguments passed to base constructor adapted from https://gist.github.com/dmeliza/3251476 """ from matplotlib.patches import Rectangle from matplotlib.offsetbox import AuxTransformBox, VPacker from matplotlib.offsetbox import HPacker, TextArea, DrawingArea import matplotlib.patches as mpatches if fontsize is None: fontsize = mpl.rcParams['font.size'] bars = AuxTransformBox(transform) if sizex and sizey: # both horizontal and vertical scalebar # hacky fix for possible misalignment errors that may occur # on small figures if ec is None: lw = 0 endpt = (sizex, 0) art = mpatches.FancyArrowPatch( (0, 0), endpt, color=ec, linewidth=lw, capstyle=capstyle, arrowstyle=mpatches.ArrowStyle.BarAB(widthA=0, widthB=lw * 2)) barsx = bars barsx.add_artist(art) endpt = (0, sizey) art = mpatches.FancyArrowPatch( (0, 0), endpt, color=ec, linewidth=lw, capstyle=capstyle, arrowstyle=mpatches.ArrowStyle.BarAB(widthA=0, widthB=lw * 2)) barsy = bars barsy.add_artist(art) else: if sizex: endpt = (sizex, 0) art = mpatches.FancyArrowPatch( (0, 0), endpt, color=ec, linewidth=lw, arrowstyle=mpatches.ArrowStyle.BarAB(widthA=lw * 2, widthB=lw * 2)) bars.add_artist(art) if sizey: endpt = (0, sizey) art = mpatches.FancyArrowPatch( (0, 0), endpt, color=ec, linewidth=lw, arrowstyle=mpatches.ArrowStyle.BarAB(widthA=lw * 2, widthB=lw * 2)) bars.add_artist(art) if xfirst: if sizex and labelx: bars = VPacker(children=[ bars, TextArea(labelx, minimumdescent=False, textprops=dict(color=fc, size=fontsize)) ], align="center", pad=pad, sep=sep) if sizey and labely: bars = HPacker(children=[ TextArea(labely, textprops=dict(color=fc, size=fontsize)), bars ], align="center", pad=pad, sep=sep) else: if sizey and labely: bars = HPacker(children=[ TextArea(labely, textprops=dict(color=fc, size=fontsize)), bars ], align="center", pad=pad, sep=sep) if sizex and labelx: bars = VPacker(children=[ bars, TextArea(labelx, minimumdescent=False, textprops=dict(color=fc, size=fontsize)) ], align="center", pad=pad, sep=sep) AnchoredOffsetbox.__init__(self, loc, pad=pad, borderpad=borderpad, child=bars, prop=prop, frameon=False, **kwargs)
def __init__(self, transform, sizex=0, sizey=0, labelx=None, labely=None, loc=4, pad=0.1, borderpad=0.1, sep=2, prop=None, xorder='text above', textprops=None, linewidth=3, bbox=[1, 1.115, 0, 0], bbox_transform=None, **kwargs): """Draw a horizontal and/or vertical bar with the size in data coordinate of the give axes. A label will be drawn underneath (center-aligned). This is from GitHubGist user dmeliza, with the code found at https://gist.github.com/dmeliza/3251476 Args: transform : the coordinate frame (typically axes.transData) sizex,sizey : width of x,y bar, in data units. 0 to omit labelx,labely : labels for x,y bars; None to omit loc : position in containing axes pad, borderpad : padding, in fraction of the legend font size (or prop) sep : separation between labels and bars in points. **kwargs : additional arguments passed to base class constructor linewidth : Thickness of the scale bar bbox : Where the scale bar should be relative to the axis """ from matplotlib.patches import Rectangle from matplotlib.offsetbox import AuxTransformBox, VPacker, HPacker, TextArea, DrawingArea bars = AuxTransformBox(transform) if sizex: bars.add_artist( Rectangle((0, 0), sizex, 0, fc="none", linewidth=linewidth)) if sizey: bars.add_artist(Rectangle((0, 0), 0, sizey, fc="none"), linewidth=linewidth) if sizex and labelx: text_area = TextArea(labelx, textprops=textprops, minimumdescent=False) if xorder == 'text above': bar_child = [text_area, bars] elif xorder == 'text below': bar_child = [bars, text_area] bars = VPacker(children=bar_child, align="right", pad=0, sep=sep) if sizey and labely: text_area = TextArea(labely, textprops=textprops) bars = HPacker(children=[text_area, bars], align="center", pad=0, sep=sep) AnchoredOffsetbox.__init__(self, loc, pad=pad, borderpad=borderpad, child=bars, prop=prop, frameon=False, bbox_transform=bbox_transform, **kwargs) AnchoredOffsetbox.set_bbox_to_anchor(self, bbox, transform=bbox_transform)