Skip to content

Instantly share code, notes, and snippets.

@MNoichl
Last active April 30, 2024 22:39
Show Gist options
  • Save MNoichl/ea8b9558659c2044f1172867a03ff77e to your computer and use it in GitHub Desktop.
Save MNoichl/ea8b9558659c2044f1172867a03ff77e to your computer and use it in GitHub Desktop.
Small function to add a coordinate system indicator without visible axes to a umap-plot
def add_inset_axes(base_ax, inset_position, inset_size, line_length_ratio=0.4):
"""
Adds an inset axis with a UMAPX/Y indicator into a Matplotlib figure.
Parameters:
- base_ax: The base axes to which the inset will be added.
- inset_position: The position of the inset in figure space (x, y).
- inset_size: Width and height of the inset in figure space (width, height).
- line_length_ratio: The ratio of the line length to the inset size.
Returns:
- ax_inset: The inset axes.
"""
# Create the inset axis with a fixed aspect ratio:
ax_inset = fig.add_axes([inset_position[0], inset_position[1], inset_size[0], inset_size[1]], aspect='equal')
ax_inset.patch.set_alpha(0) # Make inset background transparent
ax_inset.axis('off') # Turn off the inset axes
# Calculate the line lengths based on the inset size and ratio:
line_length = line_length_ratio * min(inset_size)
# Draw the x-lines & text:
ax_inset.plot([0, line_length], [0, 0], color='k', lw=4,solid_capstyle='projecting')
ax_inset.text(0, -0.003, 'UMAP-X', ha='left', va='top', fontsize=10,fontweight='bold')
# Draw the y-lines & text:
ax_inset.vlines(x=0, ymin=0, ymax=line_length, color='k', lw=4)
ax_inset.text(-0.002, 0, 'UMAP-Y', ha='right', va='bottom', fontsize=10, rotation=90,fontweight='bold')
return ax_inset
add_inset_axes(ax, (0.2,0.35), (0.05,0.05))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment