The currently accepted answer is absolutely correct, I just thought anybody finding this could use a working solution for their problem.
Using matplotlib
you can create a linear segmented colormap:
colors = ["darkorange", "gold", "lawngreen", "lightseagreen"]
cmap1 = matplotlib.colors.LinearSegmentedColormap.from_list("mycmap", colors)
If your colors shouldn't be uniformly spread along the range, you can define their position too:
colors = ["darkorange", "gold", "lawngreen", "lightseagreen"]
nodes = [0.0, 0.4, 0.8, 1.0]
cmap2 = matplotlib.colors.LinearSegmentedColormap.from_list("mycmap", list(zip(nodes, colors)))
From here you can treat this colormap the same way you would any built in linear segmented colormap, like copper. For example you can export a list of 192 colors from the range between 25%-90% of your colormap like this:
palette = [matplotlib.colors.rgb2hex(c) for c in cmap1(np.linspace(0.25, 0.9, 192))]
And then you can use this palette in whatever charting solution you prefer.
Edit:
As I also often need the exact same functionality, I just created a function for this.
The code itself:
def get_palette(cmap='Greys', n=192, start=0, end=1):
import matplotlib, numpy as np
linspace = np.linspace(start, end, n)
if isinstance(cmap, list):
cmap = matplotlib.colors.LinearSegmentedColormap.from_list("customcmap", cmap)
palette = cmap(linspace)
elif isinstance(cmap, str):
cmap = matplotlib.pyplot.cm.get_cmap(cmap)
palette = cmap(linspace)
else:
palette = cmap(linspace)
hex_palette = [matplotlib.colors.rgb2hex(c) for c in palette]
return hex_palette