From f030e9e86fd56cfb9c8db7766441da41d2d627f3 Mon Sep 17 00:00:00 2001 From: Maxim Ivanov Date: Fri, 6 Nov 2020 20:09:16 +0700 Subject: [PATCH 1/3] REF: simplify cycling through colors --- pandas/plotting/_matplotlib/style.py | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/pandas/plotting/_matplotlib/style.py b/pandas/plotting/_matplotlib/style.py index b2c7b2610845c..23c4d3e9df8a3 100644 --- a/pandas/plotting/_matplotlib/style.py +++ b/pandas/plotting/_matplotlib/style.py @@ -1,3 +1,4 @@ +import itertools from typing import ( TYPE_CHECKING, Collection, @@ -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( @@ -128,19 +129,15 @@ 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)) + for _, col in zip(range(max_colors), itertools.cycle(colors)): + yield col def _get_colors_from_colormap( From 10dff7a9854fd67f54eb0f361b13c5a0c1248c7a Mon Sep 17 00:00:00 2001 From: Maxim Ivanov Date: Sat, 7 Nov 2020 19:38:13 +0700 Subject: [PATCH 2/3] REF: avoid using placeholder variable in loop --- pandas/plotting/_matplotlib/style.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pandas/plotting/_matplotlib/style.py b/pandas/plotting/_matplotlib/style.py index 23c4d3e9df8a3..c4665be7ff2c3 100644 --- a/pandas/plotting/_matplotlib/style.py +++ b/pandas/plotting/_matplotlib/style.py @@ -136,8 +136,7 @@ def _cycle_colors(colors: List[Color], num_colors: int) -> Iterator[Color]: than needed and nothing needs to be done here. """ max_colors = max(num_colors, len(colors)) - for _, col in zip(range(max_colors), itertools.cycle(colors)): - yield col + yield from itertools.islice(itertools.cycle(colors), 0, max_colors) def _get_colors_from_colormap( From fcd4ee23c4f2f787716d29cf27b8c1f6be3175f1 Mon Sep 17 00:00:00 2001 From: Maxim Ivanov Date: Sat, 7 Nov 2020 20:07:05 +0700 Subject: [PATCH 3/3] REF: eliminate start=0 arg in islice --- pandas/plotting/_matplotlib/style.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/plotting/_matplotlib/style.py b/pandas/plotting/_matplotlib/style.py index c4665be7ff2c3..cc2dde0f2179a 100644 --- a/pandas/plotting/_matplotlib/style.py +++ b/pandas/plotting/_matplotlib/style.py @@ -136,7 +136,7 @@ def _cycle_colors(colors: List[Color], num_colors: int) -> Iterator[Color]: than needed and nothing needs to be done here. """ max_colors = max(num_colors, len(colors)) - yield from itertools.islice(itertools.cycle(colors), 0, max_colors) + yield from itertools.islice(itertools.cycle(colors), max_colors) def _get_colors_from_colormap(