code: def mplcmap_to_palette(cmap, ncolors=None, categorical=False):
      
      from matplotlib.colors import Colormap, ListedColormap
  
      ncolors = ncolors or 256
      if not isinstance(cmap, Colormap):
          import matplotlib.cm as cm
          # Alias bokeh Category cmaps with mpl tab cmaps
          if cmap.startswith('Category'):
              cmap = cmap.replace('Category', 'tab')
          try:
              cmap = cm.get_cmap(cmap)
          except:
              cmap = cm.get_cmap(cmap.lower())
      if isinstance(cmap, ListedColormap):
          if categorical:
              palette = [rgb2hex(cmap.colors[i%cmap.N]) for i in range(ncolors)]
              return palette
          elif cmap.N > ncolors:
              palette = [rgb2hex(c) for c in cmap(np.arange(cmap.N))]
              if len(palette) != ncolors:
                  palette = [palette[int(v)] for v in np.linspace(0, len(palette)-1, ncolors)]
              return palette
      return [rgb2hex(c) for c in cmap(np.linspace(0, 1, ncolors))]
docs: Converts a matplotlib colormap to palette of RGB hex strings."