def insert_ax(ax, location, shrunk=0.7, space=.05, axspace=0.02, shareax=False, **kwargs): """ insert an axis at the requested location The new axis will share the main axis x-axis (location=top or bottom) or the y-axis (location=left or right). Parameters: ----------- location: [string] top/bottom/left/right, i.e. where new axis will be set shrunk: [float] the main axis will be reduced by so much (0.7 = 70%). the new axis will take the room space: [float] extra space new axis does not use between it and the edge of the figure. (in figure unit, i.e., [0,1]) axspace: [float] extra space new axis does not use between it and the input axis. (in figure unit, i.e., [0,1]) shareax: [bool] The new axis will share the main axis x-axis (location=top or bottom) or the y-axis (location=left or right). If so, the axis ticks will be cleaned. **kwargs goes to figure.add_axes() for the new axis Returns: -------- axes (the new axis) """ # -------------------- # hist x # -------------------- # # -- keep trace of the original axes bboxorig = ax.get_position().frozen() if location in ["top", "bottom"]: axhist = ax.figure.add_axes([0.1, 0.2, 0.3, 0.4], sharex=ax if shareax else None, **kwargs) # This will be changed _bboxax = ax.get_position().shrunk(1, shrunk) _bboxhist = Bbox([[_bboxax.xmin, _bboxax.ymax + axspace], [_bboxax.xmax, bboxorig.ymax - space]]) if location == "bottom": tanslate = _bboxhist.height + space + axspace _bboxhist = _bboxhist.translated( 0, bboxorig.ymin - _bboxhist.ymin + space) _bboxax = _bboxax.translated(0, tanslate) # -------------------- # hist y # -------------------- # elif location in ["right", "left"]: axhist = ax.figure.add_axes([0.5, 0.1, 0.2, 0.42], sharey=ax if shareax else None, **kwargs) # This will be changed _bboxax = ax.get_position().shrunk(shrunk, 1) _bboxhist = Bbox([[_bboxax.xmax + axspace, _bboxax.ymin], [bboxorig.xmax - space, _bboxax.ymax]]) if location == "left": tanslate = _bboxhist.width + space + axspace _bboxhist = _bboxhist.translated( bboxorig.xmin - _bboxhist.xmin + space, 0) _bboxax = _bboxax.translated(tanslate, 0) else: raise ValueError("location must be 'top'/'bottom'/'left' or 'right'") axhist.set_position(_bboxhist) ax.set_position(_bboxax) # --------------------- # remove their ticks if shareax: if location in ["top", "right"]: [[label.set_visible(False) for label in lticks] for lticks in [axhist.get_xticklabels(), axhist.get_yticklabels()]] elif location == "bottom": [[label.set_visible(False) for label in lticks] for lticks in [ax.get_xticklabels(), axhist.get_yticklabels()]] elif location == "left": [[label.set_visible(False) for label in lticks] for lticks in [ax.get_yticklabels(), axhist.get_xticklabels()]] return axhist
def insert_ax(ax,location,shrunk=0.7,space=.05, axspace=0.02,shareax=False,**kwargs): """ insert an axis at the requested location The new axis will share the main axis x-axis (location=top or bottom) or the y-axis (location=left or right). Parameters: ----------- location: [string] top/bottom/left/right, i.e. where new axis will be set shrunk: [float] the main axis will be reduced by so much (0.7 = 70%). the new axis will take the room space: [float] extra space new axis does not use between it and the edge of the figure. (in figure unit, i.e., [0,1]) axspace: [float] extra space new axis does not use between it and the input axis. (in figure unit, i.e., [0,1]) shareax: [bool] The new axis will share the main axis x-axis (location=top or bottom) or the y-axis (location=left or right). If so, the axis ticks will be cleaned. **kwargs goes to figure.add_axes() for the new axis Returns: -------- axes (the new axis) """ # -------------------- # hist x # -------------------- # # -- keep trace of the original axes bboxorig = ax.get_position().frozen() if location in ["top","bottom"]: axhist = ax.figure.add_axes([0.1,0.2,0.3,0.4],sharex=ax if shareax else None, **kwargs) # This will be changed _bboxax = ax.get_position().shrunk(1,shrunk) _bboxhist = Bbox([[_bboxax.xmin, _bboxax.ymax+axspace ], [_bboxax.xmax, bboxorig.ymax-space]]) if location == "bottom": tanslate = _bboxhist.height + space+axspace _bboxhist = _bboxhist.translated(0, bboxorig.ymin-_bboxhist.ymin+space) _bboxax = _bboxax.translated(0,tanslate) # -------------------- # hist y # -------------------- # elif location in ["right","left"]: axhist = ax.figure.add_axes([0.5,0.1,0.2,0.42],sharey=ax if shareax else None, **kwargs) # This will be changed _bboxax = ax.get_position().shrunk(shrunk,1) _bboxhist = Bbox([[_bboxax.xmax+axspace, _bboxax.ymin ], [bboxorig.xmax-space, _bboxax.ymax]]) if location == "left": tanslate = _bboxhist.width + space + axspace _bboxhist = _bboxhist.translated(bboxorig.xmin-_bboxhist.xmin+space, 0) _bboxax = _bboxax.translated(tanslate,0) else: raise ValueError("location must be 'top'/'bottom'/'left' or 'right'") axhist.set_position(_bboxhist) ax.set_position(_bboxax) # --------------------- # remove their ticks if shareax: if location in ["top","right"]: [[label.set_visible(False) for label in lticks] for lticks in [axhist.get_xticklabels(),axhist.get_yticklabels()]] elif location == "bottom": [[label.set_visible(False) for label in lticks] for lticks in [ax.get_xticklabels(),axhist.get_yticklabels()]] elif location == "left": [[label.set_visible(False) for label in lticks] for lticks in [ax.get_yticklabels(),axhist.get_xticklabels()]] return axhist