Skip to content

Commit 393ae46

Browse files
committed
REF: extract generator function to simplify logic
1 parent 45647a4 commit 393ae46

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

pandas/plotting/_matplotlib/style.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from typing import (
22
TYPE_CHECKING,
33
Collection,
4+
Iterator,
45
List,
56
Optional,
67
Sequence,
@@ -13,9 +14,10 @@
1314
import matplotlib.colors
1415
import numpy as np
1516

16-
import pandas.core.common as com
1717
from pandas.core.dtypes.common import is_list_like
1818

19+
import pandas.core.common as com
20+
1921
if TYPE_CHECKING:
2022
from matplotlib.colors import Colormap
2123

@@ -103,25 +105,30 @@ def _get_colors_from_color(
103105
if len(color) == 0:
104106
raise ValueError(f"Invalid color argument: {color}")
105107

106-
if isinstance(color, str):
107-
if _is_single_color(color):
108-
# GH #36972
109-
return [color]
110-
else:
111-
return list(color)
108+
if isinstance(color, str) and _is_single_color(color):
109+
# GH #36972
110+
return [color]
112111

113112
if _is_floats_color(color):
114113
color = cast(Sequence[float], color)
115114
return [color]
116115

117116
color = cast(Collection[Color], color)
118-
colors = []
117+
return list(_gen_list_of_colors_from_iterable(color))
118+
119+
120+
def _gen_list_of_colors_from_iterable(color: Collection[Color]) -> Iterator[Color]:
121+
"""
122+
Yield colors from string of several letters or from collection of colors.
123+
"""
119124
for x in color:
125+
if isinstance(x, str):
126+
# to avoid warnings on upper case single letter colors
127+
x = x.lower()
120128
if _is_single_color(x):
121-
colors.append(x)
129+
yield x
122130
else:
123131
raise ValueError(f"Invalid color {x}")
124-
return colors
125132

126133

127134
def _is_floats_color(color: Union[Color, Collection[Color]]) -> bool:
@@ -172,6 +179,7 @@ def _is_single_color(color: Color) -> bool:
172179
- 'red'
173180
- 'green'
174181
- 'C3'
182+
- 'firebrick'
175183
176184
Parameters
177185
----------

0 commit comments

Comments
 (0)