MCPcopy
hub / github.com/pydata/xarray / _color_palette

Function _color_palette

xarray/plot/utils.py:123–153  ·  view source on GitHub ↗
(cmap, n_colors)

Source from the content-addressed store, hash-verified

121
122
123def _color_palette(cmap, n_colors):
124 import matplotlib.pyplot as plt
125 from matplotlib.colors import ListedColormap
126
127 colors_i = np.linspace(0, 1.0, n_colors)
128 if isinstance(cmap, list | tuple):
129 # expand or truncate the list of colors to n_colors
130 cmap = list(itertools.islice(itertools.cycle(cmap), n_colors))
131 cmap = ListedColormap(cmap)
132 pal = cmap(colors_i)
133 elif isinstance(cmap, str):
134 # we have some sort of named palette
135 try:
136 # is this a matplotlib cmap?
137 cmap = plt.get_cmap(cmap)
138 pal = cmap(colors_i)
139 except ValueError:
140 # ValueError happens when mpl doesn't like a colormap, try seaborn
141 try:
142 from seaborn import color_palette
143
144 pal = color_palette(cmap, n_colors=n_colors)
145 except (ValueError, ImportError):
146 # or maybe we just got a single color as a string
147 cmap = ListedColormap([cmap] * n_colors)
148 pal = cmap(colors_i)
149 else:
150 # cmap better be a LinearSegmentedColormap (e.g. viridis)
151 pal = cmap(colors_i)
152
153 return pal
154
155
156# _determine_cmap_params is adapted from Seaborn:

Calls 1

linspaceMethod · 0.80

Used in the wild real call sites across dependent graphs

searching dependent graphs…