Skip to content

FIX: validate empty colors sequence in LinearSegmentedColormap.from_list() - #32617

Draft
klnmurthi12345 wants to merge 1 commit into
matplotlib:mainfrom
klnmurthi12345:fix-colormap-empty-colors
Draft

klnmurthi12345 wants to merge 1 commit into
matplotlib:mainfrom
klnmurthi12345:fix-colormap-empty-colors

Conversation

@klnmurthi12345

Copy link
Copy Markdown

PR summary

Closes #32615

When calling LinearSegmentedColormap.from_list("name", []) with an empty sequence of colors, from_list() constructed a colormap with empty (0, 3) segment data tables, which caused an unhandled IndexError: index 0 is out of bounds for axis 0 with size 0 upon evaluation (cmap(0.5)).

This PR:

  • Checks if len(colors) == 0 in LinearSegmentedColormap.from_list.
  • Raises ValueError('colors must not be empty').
  • Adds regression unit test in lib/matplotlib/tests/test_colors.py.

AI Disclosure

Assisted in drafting tests and verifying against test cases.

PR quality check

  • Use an expressive title, e.g. "Fix title font property precedence"
  • New and changed code is tested
  • [N/A] Plotting related features are demonstrated in an example
  • [N/A] New features and API changes have release notes
  • Documentation complies with general and docstring guidelines

@timhoffm

Copy link
Copy Markdown
Member

Putting review on hold until we've clarified whether this is a real issue #32615 (comment).

@timhoffm
timhoffm marked this pull request as draft November 16, 2026 22:24

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: LinearSegmentedColormap.from_list crashes with IndexError when passed empty colors

2 participants