Skip to content

Commit dedd0dd

Browse files
committed
REF: extract new method _is_single_color
1. Rename _is_single_color -> _is_single_string_color 2. Extract new method _is_single_color, which checks whether the color provided is a single color, that can be either string or a sequence of floats. 3. Allow integers to be a part of float sequence color, (1, 0.4, 0.2, 0.5), for example.
1 parent f0ea701 commit dedd0dd

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

pandas/plotting/_matplotlib/style.py

+26-8
Original file line numberDiff line numberDiff line change
@@ -106,18 +106,36 @@ def _get_colors_from_color(
106106
if len(color) == 0:
107107
raise ValueError(f"Invalid color argument: {color}")
108108

109-
if isinstance(color, str) and _is_single_color(color):
110-
# GH #36972
111-
return [color]
112-
113-
if _is_floats_color(color):
114-
color = cast(Sequence[float], color)
109+
if _is_single_color(color):
110+
color = cast(Color, color)
115111
return [color]
116112

117113
color = cast(Collection[Color], color)
118114
return list(_gen_list_of_colors_from_iterable(color))
119115

120116

117+
def _is_single_color(color: Union[Color, Collection[Color]]) -> bool:
118+
"""Check if ``color`` is a single color, not a sequence of colors.
119+
120+
Single color is of these kinds:
121+
- Named color "red"
122+
- Alias "g"
123+
- Sequence of floats, such as (0.1, 0.2, 0.3) or (0.1, 0.2, 0.3, 0.4).
124+
125+
See Also
126+
--------
127+
_is_single_string_color
128+
"""
129+
if isinstance(color, str) and _is_single_string_color(color):
130+
# GH #36972
131+
return True
132+
133+
if _is_floats_color(color):
134+
return True
135+
136+
return False
137+
138+
121139
def _gen_list_of_colors_from_iterable(color: Collection[Color]) -> Iterator[Color]:
122140
"""
123141
Yield colors from string of several letters or from collection of colors.
@@ -134,7 +152,7 @@ def _is_floats_color(color: Union[Color, Collection[Color]]) -> bool:
134152
return bool(
135153
is_list_like(color)
136154
and (len(color) == 3 or len(color) == 4)
137-
and all(isinstance(x, float) for x in color)
155+
and all(isinstance(x, (int, float)) for x in color)
138156
)
139157

140158

@@ -168,7 +186,7 @@ def _random_color(column: int) -> List[float]:
168186
return rs.rand(3).tolist()
169187

170188

171-
def _is_single_color(color: Color) -> bool:
189+
def _is_single_string_color(color: Color) -> bool:
172190
"""Check if ``color`` is a single color.
173191
174192
Examples of single colors:

0 commit comments

Comments
 (0)