如何在 matplotlib 中使离散颜色图连续?

How to make discrete colormap contiguous in matplotlib?

如何在 matplotlib 中将离散颜色图转换为连续颜色图?我需要这样的东西:

import matplotlib.colors as colors
cm_d = colormap = colors.ListedColormap(["#ffffff", "#ffcdcd", "#ff989b", "#ff6669", "#ff1818", "#d20000", "#a30002", "#720001", "#500002", "#670061", "#5e1a8b", "#3d0064", "#555555", "#343434", "#000000"])
cm_c = do_some_magic(cm_d)

离散颜色图 (cm_d):

连续色图(cm_c):

获取连续色图 你想调用这个方法: https://matplotlib.org/stable/api/_as_gen/matplotlib.colors.LinearSegmentedColormap.html#matplotlib.colors.LinearSegmentedColormap.from_list

Static method; factory function for generating a smoothly-varying LinearSegmentedColormap.