Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 44b9873

Browse filesBrowse files
committed
MNT: Refactor from_levels_and_colors()
Primary motivation was to remove the N parameter to ListedColormap (in preparation of its deprecation #28763 (comment) -2322791660). That parameter is not needed here because `len(colors[color_slice]) == n_data_colors)`, otherwise the ValueError above would have raised. Hopefully, this is overall more readable.
1 parent ca39d41 commit 44b9873
Copy full SHA for 44b9873

File tree

Expand file treeCollapse file tree

1 file changed

+11
-15
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+11
-15
lines changed

‎lib/matplotlib/colors.py

Copy file name to clipboardExpand all lines: lib/matplotlib/colors.py
+11-15Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3690,23 +3690,19 @@ def from_levels_and_colors(levels, colors, extend='neither'):
36903690
color_slice = slice_map[extend]
36913691

36923692
n_data_colors = len(levels) - 1
3693-
n_expected = n_data_colors + color_slice.start - (color_slice.stop or 0)
3693+
n_extend_colors = color_slice.start - (color_slice.stop or 0) # 0, 1 or 2
3694+
n_expected = n_data_colors + n_extend_colors
36943695
if len(colors) != n_expected:
36953696
raise ValueError(
3696-
f'With extend == {extend!r} and {len(levels)} levels, '
3697-
f'expected {n_expected} colors, but got {len(colors)}')
3698-
3699-
cmap = ListedColormap(colors[color_slice], N=n_data_colors)
3700-
3701-
if extend in ['min', 'both']:
3702-
cmap.set_under(colors[0])
3703-
else:
3704-
cmap.set_under('none')
3705-
3706-
if extend in ['max', 'both']:
3707-
cmap.set_over(colors[-1])
3708-
else:
3709-
cmap.set_over('none')
3697+
f'Expected {n_expected} colors ({n_data_colors} colors for {len(levels)} '
3698+
f'levels, and {n_extend_colors} colors for extend == {extend!r}), '
3699+
f'but got {len(colors)}')
3700+
3701+
data_colors = colors[color_slice]
3702+
under_color = colors[0] if extend in ['min', 'both'] else 'none'
3703+
over_color = colors[-1] if extend in ['max', 'both'] else 'none'
3704+
cmap = ListedColormap(data_colors).with_extremes(
3705+
under=under_color, over=over_color)
37103706

37113707
cmap.colorbar_extend = extend
37123708

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.