Last active
August 27, 2020 09:20
-
-
Save KerryHalupka/73af99afa8d4a4b7aaf1c05562ed4223 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_continuous_cmap(hex_list, float_list=None): | |
''' creates and returns a color map that can be used in heat map figures. | |
If float_list is not provided, colour map graduates linearly between each color in hex_list. | |
If float_list is provided, each color in hex_list is mapped to the respective location in float_list. | |
Parameters | |
---------- | |
hex_list: list of hex code strings | |
float_list: list of floats between 0 and 1, same length as hex_list. Must start with 0 and end with 1. | |
Returns | |
---------- | |
colour map''' | |
rgb_list = [rgb_to_dec(hex_to_rgb(i)) for i in hex_list] | |
if float_list: | |
pass | |
else: | |
float_list = list(np.linspace(0,1,len(rgb_list))) | |
cdict = dict() | |
for num, col in enumerate(['red', 'green', 'blue']): | |
col_list = [[float_list[i], rgb_list[i][num], rgb_list[i][num]] for i in range(len(float_list))] | |
cdict[col] = col_list | |
cmp = mcolors.LinearSegmentedColormap('my_cmp', segmentdata=cdict, N=256) | |
return cmp | |
x, y = np.mgrid[-5:5:0.05, -5:5:0.05] | |
z = (np.sqrt(x**2 + y**2) + np.sin(x**2 + y**2)) | |
hex_list = ['#0091ad', '#3fcdda', '#83f9f8', '#d6f6eb', '#fdf1d2', '#f8eaad', '#faaaae', '#ff57bb'] | |
fig, ax = plt.subplots(1,1) | |
im = ax.imshow(z, cmap=get_continuous_cmap(hex_list)) | |
fig.colorbar(im) | |
ax.yaxis.set_major_locator(plt.NullLocator()) # remove y axis ticks | |
ax.xaxis.set_major_locator(plt.NullLocator()) # remove x axis ticks | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment