def draw_from_point_to_bbox(parent_axes, insert_axes, point, loc=1, **kwargs): from mpl_toolkits.axes_grid1.inset_locator import TransformedBbox, BboxConnector, Bbox rect = TransformedBbox(Bbox([point, point]), parent_axes.transData) # rect = TransformedBbox(Bbox([[1, 0], [1, 0]]), parent_axes.transData) p1 = BboxConnector(rect, insert_axes.bbox, loc, **kwargs) parent_axes.add_patch(p1) p1.set_clip_on(False) return p1
def draw_from_point_to_point(parent_axes: Axes, insert_axes: Axes, point1: Sequence, point2: Sequence, **kwargs): """ add a box connector from a point in on axes to a point in another axes """ from mpl_toolkits.axes_grid1.inset_locator import TransformedBbox, BboxConnector, Bbox rect = TransformedBbox(Bbox([point1, point1]), parent_axes.transData) rect2 = TransformedBbox(Bbox([point2, point2]), insert_axes.transData) # rect = TransformedBbox(Bbox([[1, 0], [1, 0]]), parent_axes.transData) loc = 1 p1 = BboxConnector(rect, rect2, loc, **kwargs) parent_axes.add_patch(p1) p1.set_clip_on(False) return p1
def mark_inset(parent_axes, inset_axes, loc1=1, loc2=2, **kwargs): from mpl_toolkits.axes_grid1.inset_locator import TransformedBbox, BboxPatch, BboxConnector try: loc1a, loc1b = loc1 except: loc1a = loc1 loc1b = loc1 try: loc2a, loc2b = loc2 except: loc2a = loc2 loc2b = loc2 rect = TransformedBbox(inset_axes.viewLim, parent_axes.transData) pp = BboxPatch(rect, fill=False, **kwargs) parent_axes.add_patch(pp) pp.set_clip_on(False) p1 = BboxConnector(inset_axes.bbox, rect, loc1=loc1a, loc2=loc1b, **kwargs) inset_axes.add_patch(p1) p1.set_clip_on(False) p2 = BboxConnector(inset_axes.bbox, rect, loc1=loc2a, loc2=loc2b, **kwargs) inset_axes.add_patch(p2) p2.set_clip_on(False) return pp, p1, p2
def mark_inset(ax, ax2, m, m2, MAP, loc1=(1, 2), loc2=(3, 4), **kwargs): """ https://stackoverflow.com/questions/41610834/basemap-projection-geos-controlling-mark-inset-location Patched mark_inset to work with Basemap. Reason: Basemap converts Geographic (lon/lat) to Map Projection (x/y) coordinates Additionally: set connector locations separately for both axes: loc1 & loc2: tuple defining start and end-locations of connector 1 & 2 """ axzoom_geoLims = (MAP['cyl'][2:], MAP['cyl'][:2]) rect = TransformedBbox(Bbox(np.array(m(*axzoom_geoLims)).T), ax.transData) pp = BboxPatch(rect, fill=False, **kwargs) ax.add_patch(pp) p1 = BboxConnector(ax2.bbox, rect, loc1=loc1[0], loc2=loc1[1], **kwargs) ax2.add_patch(p1) p1.set_clip_on(False) p2 = BboxConnector(ax2.bbox, rect, loc1=loc2[0], loc2=loc2[1], **kwargs) ax2.add_patch(p2) p2.set_clip_on(False) return pp, p1, p2
def connect_bbox(bbox1, bbox2, loc1a, loc2a, loc1b, loc2b, prop_lines, prop_patches=None): if prop_patches is None: prop_patches = { **prop_lines, "alpha": prop_lines.get("alpha", 1) * 0.2, } c1 = BboxConnector(bbox1, bbox2, loc1=loc1a, loc2=loc2a, **prop_lines) c1.set_clip_on(False) c2 = BboxConnector(bbox1, bbox2, loc1=loc1b, loc2=loc2b, **prop_lines) c2.set_clip_on(False) bbox_patch1 = BboxPatch(bbox1, **prop_patches) bbox_patch2 = BboxPatch(bbox2, **prop_patches) p = BboxConnectorPatch( bbox1, bbox2, # loc1a=3, loc2a=2, loc1b=4, loc2b=1, loc1a=loc1a, loc2a=loc2a, loc1b=loc1b, loc2b=loc2b, **prop_patches) p.set_clip_on(False) return c1, c2, bbox_patch1, bbox_patch2, p
def mark_inset(parent_axes: Axes, inset_axes: Axes, loc1: Union[int, Sequence[int]] = 1, loc2: Union[int, Sequence[int]] = 2, **kwargs): """ like the mark_inset function from matplotlib, but loc can also be a tuple """ from mpl_toolkits.axes_grid1.inset_locator import TransformedBbox, BboxPatch, BboxConnector try: loc1a, loc1b = loc1 except: loc1a = loc1 loc1b = loc1 try: loc2a, loc2b = loc2 except: loc2a = loc2 loc2b = loc2 rect = TransformedBbox(inset_axes.viewLim, parent_axes.transData) pp = BboxPatch(rect, fill=False, **kwargs) parent_axes.add_patch(pp) pp.set_clip_on(False) p1 = BboxConnector(inset_axes.bbox, rect, loc1=loc1a, loc2=loc1b, **kwargs) inset_axes.add_patch(p1) p1.set_clip_on(False) p2 = BboxConnector(inset_axes.bbox, rect, loc1=loc2a, loc2=loc2b, **kwargs) inset_axes.add_patch(p2) p2.set_clip_on(False) return pp, p1, p2
def mark_inset(parent_axes, inset_axes, loc1a=1, loc1b=1, loc2a=2, loc2b=2, **kwargs): rect = TransformedBbox(inset_axes.viewLim, parent_axes.transData) pp = BboxPatch(rect, fill=False, linewidth=.5, **kwargs) parent_axes.add_patch(pp) p1 = BboxConnector(inset_axes.bbox, rect, loc1=loc1a, loc2=loc1b, linewidth=.5, **kwargs) inset_axes.add_patch(p1) p1.set_clip_on(False) p2 = BboxConnector(inset_axes.bbox, rect, loc1=loc2a, loc2=loc2b, linewidth=.5, **kwargs) inset_axes.add_patch(p2) p2.set_clip_on(False) return pp, p1, p2
def connect_bbox(bbox1, bbox2, loc1a, loc2a, loc1b, loc2b, prop_lines, prop_patches=None): if prop_patches is None: prop_patches = prop_lines.copy() prop_patches["alpha"] = prop_patches.get("alpha", 1) * 0.2 c1 = BboxConnector(bbox1, bbox2, loc1=loc1a, loc2=loc2a, **prop_lines) c1.set_clip_on(False) c2 = BboxConnector(bbox1, bbox2, loc1=loc1b, loc2=loc2b, **prop_lines) c2.set_clip_on(False) bbox1_patch = BboxPatch(bbox1, **prop_patches) bbox2_patch = BboxPatch(bbox2, **prop_patches) # connects two bbox-es with a quadrilateral p = BboxConnectorPatch(bbox1, bbox2, loc1a=loc1a, loc2a=loc2a, loc1b=loc1b, loc2b=loc2b, **prop_patches) p.set_fill(True) p.set_clip_on(False) return c1, c2, bbox1_patch, bbox2_patch, p
def mark_inset_specify_transforms(parent_axes, inset_axes, loc1, loc2, transform=None, **kwargs): if transform is None: transform = parent_axes.transData from matplotlib.transforms import TransformedBbox from mpl_toolkits.axes_grid1.inset_locator import BboxPatch, BboxConnector print('viewlims: ', inset_axes.viewLim, transform.transform(inset_axes.viewLim)) rect = TransformedBbox(inset_axes.viewLim, transform) fill = kwargs.pop("fill", False) pp = BboxPatch(rect, fill=fill, **kwargs) parent_axes.add_patch(pp) p1 = BboxConnector(inset_axes.bbox, rect, loc1=loc1, **kwargs) inset_axes.add_patch(p1) p1.set_clip_on(False) p2 = BboxConnector(inset_axes.bbox, rect, loc1=loc2, **kwargs) inset_axes.add_patch(p2) p2.set_clip_on(False) return pp, p1, p2
def mark_inset_mod(parent_axes, inset_axes, loc11, loc12, loc21=None, loc22=None, **kwargs): rect = TransformedBbox(inset_axes.viewLim, parent_axes.transData) if 'fill' in kwargs: pp = BboxPatch(rect, **kwargs) else: fill = bool({'fc', 'facecolor', 'color'}.intersection(kwargs)) pp = BboxPatch(rect, fill=fill, **kwargs) parent_axes.add_patch(pp) p1 = BboxConnector(inset_axes.bbox, rect, loc1=loc11, loc2=loc21, **kwargs) inset_axes.add_patch(p1) p1.set_clip_on(False) p2 = BboxConnector(inset_axes.bbox, rect, loc1=loc12, loc2=loc22, **kwargs) inset_axes.add_patch(p2) p2.set_clip_on(False) return pp, p1, p2
def mark_geo_inset(ax, ax2, m, m2, loc1=(1, 2), loc2=(3, 4), **kwargs): """ Patched mark_inset to work with Basemap. Reason: Basemap converts Geographic (lon/lat) to Map Projection (x/y) coordinates See: https://stackoverflow.com/questions/41610834/basemap-projection-geos-controlling-mark-inset-location Args: ax (subplot): parent subplot where inset is marked on parent map ax2 (subplot): inset subplot where inset map is displayed m (basemap): parent basemap associated with ax m2 (basemap): inset basemap associated with ax2 loc1 (tuple): start and end locations of connector 1 loc2 (tuple): start and end locations of connector 2 **kwargs (optional args): to manage the presentation of the connectors and inset markers loc tuple values: 'upper right' : 1, 'upper left' : 2, 'lower left' : 3, 'lower right' : 4 Returns: BboxPatch: Patch for the inset coverage on the parent subplot BboxConnector: Connector line 1 connecting the coverage rectangle on the parent subplot to the inset map BboxConnector: Connector line 2 connecting the coverage rectanlge on the parent subplot to the inset map """ # Doesn't work for Basemap # rect = TransformedBbox(inset_axes.viewLim, parent_axes.transData) # axzoom_geoLims = np.array(m2(*ax2.viewLim._points.T, inverse=True)) axzoom_geoLims = m2(ax2.get_xlim(), ax2.get_ylim(), inverse=True) rect = TransformedBbox(Bbox(np.array(m(*axzoom_geoLims)).T), ax.transData) pp = BboxPatch(rect, fill=False, **kwargs) ax.add_patch(pp) p1 = BboxConnector(ax2.bbox, rect, loc1=loc1[0], loc2=loc1[1], **kwargs) ax2.add_patch(p1) p1.set_clip_on(False) p2 = BboxConnector(ax2.bbox, rect, loc1=loc2[0], loc2=loc2[1], **kwargs) ax2.add_patch(p2) p2.set_clip_on(False) return pp, p1, p2
def _mark_inset(parent_axes, inset_axes, loc1a = 1, loc1b = 1, loc2a = 2, loc2b = 2, **kwargs): # Draw a bbox of the region of the inset axes in the parent axes and # connecting lines between the bbox and the inset axes area # loc1, loc2 : {1, 2, 3, 4} rect = TransformedBbox(inset_axes.viewLim, parent_axes.transData) pp = BboxPatch(rect, fill = False, **kwargs) parent_axes.add_patch(pp) p1 = BboxConnector(inset_axes.bbox, rect, loc1 = loc1a, loc2 = loc1b, **kwargs) inset_axes.add_patch(p1) p1.set_clip_on(False) p2 = BboxConnector(inset_axes.bbox, rect, loc1 = loc2a, loc2 = loc2b, **kwargs) inset_axes.add_patch(p2) p2.set_clip_on(False) print('\nInset created') return pp, p1, p2
def connect_bbox(bbox1, bbox2, loc1a, loc2a, loc1b, loc2b, prop_lines, prop_patches=None): """ Connect two bounding boxes """ if prop_patches is None: prop_patches = prop_lines.copy() prop_patches["alpha"] = prop_patches.get("alpha", 1) * 0.2 c1 = BboxConnector(bbox1, bbox2, loc1=loc1a, loc2=loc2a, **prop_lines) c1.set_clip_on(False) c2 = BboxConnector(bbox1, bbox2, loc1=loc1b, loc2=loc2b, **prop_lines) c2.set_clip_on(False) bbox_patch1 = BboxPatch(bbox1, **prop_patches) bbox_patch2 = BboxPatch(bbox2, **prop_patches) patch = BboxConnectorPatch(bbox1, bbox2, loc1a=loc1a, loc2a=loc2a, loc1b=loc1b, loc2b=loc2b, **prop_patches) patch.set_clip_on(False) return c1, c2, bbox_patch1, bbox_patch2, patch
def cust_mark_inset(parent_axes, inset_axes, x1,x2,y1,y2, loc11,loc12,loc21,loc22,**kwargs): print parent_axes.transData print inset_axes.viewLim rect = TransformedBbox(Bbox.from_extents([x1,y1,x2,y2]), parent_axes.transData) pp = BboxPatch(rect, **kwargs) parent_axes.add_patch(pp) p1 = BboxConnector(inset_axes.bbox, rect, loc1=loc11,loc2=loc12, **kwargs) inset_axes.add_patch(p1) p1.set_clip_on(False) p2 = BboxConnector(inset_axes.bbox, rect, loc1=loc21,loc2=loc22, **kwargs) inset_axes.add_patch(p2) p2.set_clip_on(False)
def connect_bbox(bbox1, bbox2, loc1a, loc2a, loc1b, loc2b, prop_lines, prop_patches=None, prop_patches2_update=None): if prop_patches is None: prop_patches = prop_lines.copy() prop_patches["alpha"] = prop_patches.get("alpha", 1) * 0.2 prop_patches2 = prop_patches.copy() if prop_patches2_update: prop_patches2.update(prop_patches2_update) c1 = BboxConnector(bbox1, bbox2, loc1=loc1a, loc2=loc2a, **prop_lines) c1.set_clip_on(False) c2 = BboxConnector(bbox1, bbox2, loc1=loc1b, loc2=loc2b, **prop_lines) c2.set_clip_on(False) bbox_patch1 = BboxPatch(bbox1, **prop_patches2) bbox_patch2 = BboxPatch(bbox2, **prop_patches) p = BboxConnectorPatch( bbox1, bbox2, #loc1a=3, loc2a=2, loc1b=4, loc2b=1, loc1a=loc1a, loc2a=loc2a, loc1b=loc1b, loc2b=loc2b, **prop_patches) p.set_clip_on(False) return c1, c2, bbox_patch1, bbox_patch2, p
def connect_bbox(bbox1, bbox2, loc1a, loc2a, loc1b, loc2b, prop_lines, prop_patches=None): if prop_patches is None: prop_patches = prop_lines.copy() prop_patches["alpha"] = prop_patches.get("alpha", 1)*0.2 c1 = BboxConnector(bbox1, bbox2, loc1=loc1a, loc2=loc2a, **prop_lines) c1.set_clip_on(False) c2 = BboxConnector(bbox1, bbox2, loc1=loc1b, loc2=loc2b, **prop_lines) c2.set_clip_on(False) bbox_patch1 = BboxPatch(bbox1, **prop_patches) bbox_patch2 = BboxPatch(bbox2, **prop_patches) p = BboxConnectorPatch(bbox1, bbox2, loc1a=loc1a, loc2a=loc2a, loc1b=loc1b, loc2b=loc2b, **prop_patches) p.set_clip_on(False) return c1, c2, bbox_patch1, bbox_patch2, p
def mark_inset(parent_axes, inset_axes, loc1a=1, loc1b=1, loc2a=2, loc2b=2, **kwargs): # Draw a bbox of the region of the inset axes in the parent axes and # connecting lines between the bbox and the inset axes area # loc1, loc2 : {1, 2, 3, 4} from mpl_toolkits.axes_grid1.inset_locator import TransformedBbox, BboxPatch, BboxConnector rect = TransformedBbox(inset_axes.viewLim, parent_axes.transData) pp = BboxPatch(rect, fill=False, **kwargs) parent_axes.add_patch(pp) p1 = BboxConnector(inset_axes.bbox, rect, loc1=loc1a, loc2=loc1b, **kwargs) inset_axes.add_patch(p1) p1.set_clip_on(False) p2 = BboxConnector(inset_axes.bbox, rect, loc1=loc2a, loc2=loc2b, **kwargs) inset_axes.add_patch(p2) p2.set_clip_on(False) return pp, p1, p2
def mark_inset(parent_axes, inset_axes, loc1a=1, loc1b=1, loc2a=2, loc2b=2, **kwargs): """ Redefined the mark_inset function. Gives freedom to connect any corner of an inset plot to any corner of the parent plot. """ rect = TransformedBbox(inset_axes.viewLim, parent_axes.transData) pp = BboxPatch(rect, fill=False, **kwargs) parent_axes.add_patch(pp) p1 = BboxConnector(inset_axes.bbox, rect, loc1=loc1a, loc2=loc1b, **kwargs) inset_axes.add_patch(p1) p1.set_clip_on(False) p2 = BboxConnector(inset_axes.bbox, rect, loc1=loc2a, loc2=loc2b, **kwargs) inset_axes.add_patch(p2) p2.set_clip_on(False) return pp, p1, p2
def connect_bbox(bbox1, bbox2, loc1a, loc2a, loc1b, loc2b, prop_lines, prop_patches=None): if prop_patches is None: prop_patches = prop_lines.copy() prop_patches["alpha"] = prop_patches.get("alpha", 1)*0.2 c1 = BboxConnector(bbox1, bbox2, loc1=loc1a, loc2=loc2a, **prop_lines) c1.set_clip_on(False) c2 = BboxConnector(bbox1, bbox2, loc1=loc1b, loc2=loc2b, **prop_lines) c2.set_clip_on(False) bbox1_patch= BboxPatch(bbox1, **prop_patches) bbox2_patch = BboxPatch(bbox2, **prop_patches) # connects two bbox-es with a quadrilateral p = BboxConnectorPatch(bbox1, bbox2, loc1a=loc1a, loc2a=loc2a, loc1b=loc1b, loc2b=loc2b, **prop_patches) p.set_fill(True) p.set_clip_on(False) return c1, c2, bbox1_patch, bbox2_patch, p
trt = tr.transform_to(parent_ax.wcs.wcs.radesys.lower()) (rx1, ry1), (rx2, ry2) = (parent_ax.wcs.wcs_world2pix( [[blt.ra.deg, blt.dec.deg]], 0)[0], parent_ax.wcs.wcs_world2pix( [[trt.ra.deg, trt.dec.deg]], 0)[0]) bbox = Bbox(np.array([(rx1, ry1), (rx2, ry2)])) rect = TransformedBbox(bbox, parent_ax.transData) markinkwargs = dict(fc='none', ec='0.5') pp = BboxPatch(rect, fill=False, **markinkwargs) parent_ax.add_patch(pp) p1 = BboxConnector(axins.bbox, rect, loc1=ZR['l1'], **markinkwargs) axins.add_patch(p1) p1.set_clip_on(False) p2 = BboxConnector(axins.bbox, rect, loc1=ZR['l2'], **markinkwargs) axins.add_patch(p2) p2.set_clip_on(False) if 'scalebarpos' in ZR: make_scalebar( axins, ZR['scalebarpos'], length=(5000 * u.au / distance).to(