Skip to content

Commit a347bc1

Browse files
authored
REF: simplify cycling through colors (#37664)
1 parent ae54325 commit a347bc1

File tree

1 file changed

+6
-10
lines changed

1 file changed

+6
-10
lines changed

pandas/plotting/_matplotlib/style.py

+6-10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import itertools
12
from typing import (
23
TYPE_CHECKING,
34
Collection,
@@ -74,7 +75,7 @@ def get_standard_colors(
7475
num_colors=num_colors,
7576
)
7677

77-
return _cycle_colors(colors, num_colors=num_colors)
78+
return list(_cycle_colors(colors, num_colors=num_colors))
7879

7980

8081
def _derive_colors(
@@ -128,19 +129,14 @@ def _derive_colors(
128129
return _get_colors_from_color_type(color_type, num_colors=num_colors)
129130

130131

131-
def _cycle_colors(colors: List[Color], num_colors: int) -> List[Color]:
132-
"""Append more colors by cycling if there is not enough color.
132+
def _cycle_colors(colors: List[Color], num_colors: int) -> Iterator[Color]:
133+
"""Cycle colors until achieving max of `num_colors` or length of `colors`.
133134
134135
Extra colors will be ignored by matplotlib if there are more colors
135136
than needed and nothing needs to be done here.
136137
"""
137-
if len(colors) < num_colors:
138-
multiple = num_colors // len(colors) - 1
139-
mod = num_colors % len(colors)
140-
colors += multiple * colors
141-
colors += colors[:mod]
142-
143-
return colors
138+
max_colors = max(num_colors, len(colors))
139+
yield from itertools.islice(itertools.cycle(colors), max_colors)
144140

145141

146142
def _get_colors_from_colormap(

0 commit comments

Comments
 (0)