Created
August 10, 2024 16:08
-
-
Save cvanelteren/56b023476ad4c270564f4a98bd39e109 to your computer and use it in GitHub Desktop.
vibrant colormap
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
import matplotlib.pyplot as plt | |
import matplotlib.colors as mcolors | |
import numpy as np | |
def create_continuous_colormap(colors, name='custom_colormap'): | |
# Define color positions (evenly spaced between 0 and 1) | |
n = len(colors) | |
positions = np.linspace(0, 1, n) | |
# Create a colormap | |
cmap = mcolors.LinearSegmentedColormap.from_list(name, list(zip(positions, colors))) | |
return cmap | |
# Test the colormap | |
def plot_colormap(cmap): | |
gradient = np.linspace(0, 1, 256).reshape(1, -1) | |
fig, ax = plt.subplots() | |
ax.imshow(gradient, aspect='auto', cmap=cmap) | |
ax.axis('off') | |
plt.show(block = 1) | |
# Example colors | |
discrete_colors = [ | |
"#003049", | |
"#d62828", | |
"#f77f00", | |
"#fcbf49", | |
"#eae2b7", | |
] | |
discrete_colors = [ | |
"#001219", | |
"#005f73", | |
"#0a9396", | |
"#94d2bd", | |
"#e9d8a6", | |
"#ee9b00", | |
"#ca6702", | |
"#bb3e03", | |
"#ae2012", | |
"#9b2226" | |
] | |
# Create the colormap | |
cmap = create_continuous_colormap(discrete_colors, 'vibrant') | |
# Plot the continuous colormap | |
plot_colormap(cmap) |
Author
cvanelteren
commented
Aug 10, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment