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

Function _build_discrete_cmap

xarray/plot/utils.py:74–120  ·  view source on GitHub ↗

Build a discrete colormap and normalization of the data.

(cmap, levels, extend, filled)

Source from the content-addressed store, hash-verified

72
73
74def _build_discrete_cmap(cmap, levels, extend, filled):
75 """
76 Build a discrete colormap and normalization of the data.
77 """
78 import matplotlib as mpl
79
80 if len(levels) == 1:
81 levels = [levels[0], levels[0]]
82
83 if not filled:
84 # non-filled contour plots
85 extend = "max"
86
87 if extend == "both":
88 ext_n = 2
89 elif extend in ["min", "max"]:
90 ext_n = 1
91 else:
92 ext_n = 0
93
94 n_colors = len(levels) + ext_n - 1
95 pal = _color_palette(cmap, n_colors)
96
97 new_cmap, cnorm = mpl.colors.from_levels_and_colors(levels, pal, extend=extend)
98 # copy the old cmap name, for easier testing
99 new_cmap.name = getattr(cmap, "name", cmap)
100
101 # copy colors to use for bad, under, and over values in case they have been
102 # set to non-default values
103 if isinstance(cmap, mpl.colors.Colormap):
104 bad = cmap(np.nan)
105
106 # Only update under and over if they were explicitly changed by the user
107 # (i.e. are different from the lowest or highest values in cmap). Otherwise
108 # leave unchanged so new_cmap uses its default values (its own lowest and
109 # highest values).
110 under = cmap(-np.inf)
111 if under == cmap(0):
112 under = None
113
114 over = cmap(np.inf)
115 if over == cmap(cmap.N - 1):
116 over = None
117
118 new_cmap = new_cmap.with_extremes(bad=bad, under=under, over=over)
119
120 return new_cmap, cnorm
121
122
123def _color_palette(cmap, n_colors):

Callers 2

_determine_cmap_paramsFunction · 0.85

Calls 1

_color_paletteFunction · 0.85

Tested by 1

Used in the wild real call sites across dependent graphs

searching dependent graphs…