Skip to content

Instantly share code, notes, and snippets.

@JohannesBuchner
Created April 19, 2023 09:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JohannesBuchner/fad4ece9816f0123fbd61b8d9cc7746f to your computer and use it in GitHub Desktop.
Save JohannesBuchner/fad4ece9816f0123fbd61b8d9cc7746f to your computer and use it in GitHub Desktop.
Manual colorbar with range, shown as a small inset with label
import matplotlib as mpl
import matplotlib.pyplot as plt
# make a colormap that can be used to look up colors
norm = mpl.colors.Normalize(vmin=0, vmax=3)
zcmap = plt.cm.ScalarMappable(norm=norm, cmap=plt.cm.viridis)
# use it to plot stuff
plt.errorbar(
x=1, xerr=0.1, y=2, yerr=0.2,
color=zcmap.to_rgba(redshifts[i]),
)
# show the color bar in the panel
cax = plt.axes([0.85, 0.5, 0.02, 0.2]) # left, bottom, width, height
cbar = plt.colorbar(zcmap, orientation="vertical", cax=cax)
cbar.set_label('redshift')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment