Skip to content

REF: simplify cycling through colors #37664

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 8, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletions pandas/plotting/_matplotlib/style.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import itertools
from typing import (
TYPE_CHECKING,
Collection,
Expand Down Expand Up @@ -74,7 +75,7 @@ def get_standard_colors(
num_colors=num_colors,
)

return _cycle_colors(colors, num_colors=num_colors)
return list(_cycle_colors(colors, num_colors=num_colors))


def _derive_colors(
Expand Down Expand Up @@ -128,19 +129,14 @@ def _derive_colors(
return _get_colors_from_color_type(color_type, num_colors=num_colors)


def _cycle_colors(colors: List[Color], num_colors: int) -> List[Color]:
"""Append more colors by cycling if there is not enough color.
def _cycle_colors(colors: List[Color], num_colors: int) -> Iterator[Color]:
"""Cycle colors until achieving max of `num_colors` or length of `colors`.

Extra colors will be ignored by matplotlib if there are more colors
than needed and nothing needs to be done here.
"""
if len(colors) < num_colors:
multiple = num_colors // len(colors) - 1
mod = num_colors % len(colors)
colors += multiple * colors
colors += colors[:mod]

return colors
max_colors = max(num_colors, len(colors))
yield from itertools.islice(itertools.cycle(colors), max_colors)


def _get_colors_from_colormap(
Expand Down