Skip to content

Instantly share code, notes, and snippets.

@autocorr
Created March 28, 2023 23:06
Show Gist options
  • Save autocorr/359a928b9a9d885f89d58196dc60e300 to your computer and use it in GitHub Desktop.
Save autocorr/359a928b9a9d885f89d58196dc60e300 to your computer and use it in GitHub Desktop.
Create a concatenated colorbar. Useful for "grayscale plus heat."
# By Eric Koch
def half_table(table1_name, table2_name,
reverse1=False, reverse2=False,
pivot=0.5):
"""
Splice together two matplotlib color tables.
"""
n_col = 256
cmap1 = cm.get_cmap(table1_name, n_col)
cmap2 = cm.get_cmap(table2_name, n_col)
pivot_int = int(round(pivot*n_col))
if pivot_int < 0:
pivot_int = 0
if pivot_int > 255:
pivot_int = 255
n_cmap1 = pivot_int
n_cmap2 = n_col-pivot_int
# initialize
newcolors = cmap1(np.linspace(0,1,n_col))
# now hack
newcolors[:pivot_int,:] = cmap1(np.linspace(0, 1, n_cmap1))
newcolors[pivot_int:,:] = cmap2(np.linspace(0, 1, n_cmap2))
newcmp = ListedColormap(newcolors)
return(newcmp)
def noise_and_heat():
return(half_table('Greys','hot', pivot=0.2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment