@@ -32,10 +32,42 @@ def get_standard_colors(
32
32
color_type : str = "default" ,
33
33
color : Optional [Union [Dict [str , Color ], Color , Collection [Color ]]] = None ,
34
34
):
35
+ """
36
+ Get standard colors based on `colormap`, `color_type` or `color` inputs.
37
+
38
+ Parameters
39
+ ----------
40
+ num_colors : int
41
+ Minimum number of colors to be returned.
42
+ Ignored if `color` is a dictionary.
43
+ colormap : :py:class:`matplotlib.colors.Colormap`, optional
44
+ Matplotlib colormap.
45
+ When provided, the resulting colors will be derived from the colormap.
46
+ color_type : {"default", "random"}, optional
47
+ Type of colors to derive. Used if provided `color` and `colormap` are None.
48
+ Ignored if either `color` or `colormap` are not None.
49
+ color : dict or str or sequence, optional
50
+ Color(s) to be used for deriving sequence of colors.
51
+ Can be either be a dictionary, or a single color (single color string,
52
+ or sequence of floats representing a single color),
53
+ or a sequence of colors.
54
+
55
+ Returns
56
+ -------
57
+ dict or list
58
+ Standard colors. Can either be a mapping if `color` was a dictionary,
59
+ or a list of colors with a length of `num_colors` or more.
60
+
61
+ Warns
62
+ -----
63
+ UserWarning
64
+ If both `colormap` and `color` are provided.
65
+ Parameter `color` will override.
66
+ """
35
67
if isinstance (color , dict ):
36
68
return color
37
69
38
- colors = _get_colors (
70
+ colors = _derive_colors (
39
71
color = color ,
40
72
colormap = colormap ,
41
73
color_type = color_type ,
@@ -45,14 +77,45 @@ def get_standard_colors(
45
77
return _cycle_colors (colors , num_colors = num_colors )
46
78
47
79
48
- def _get_colors (
80
+ def _derive_colors (
49
81
* ,
50
82
color : Optional [Union [Color , Collection [Color ]]],
51
83
colormap : Optional [Union [str , "Colormap" ]],
52
84
color_type : str ,
53
85
num_colors : int ,
54
86
) -> List [Color ]:
55
- """Get colors from user input."""
87
+ """
88
+ Derive colors from either `colormap`, `color_type` or `color` inputs.
89
+
90
+ Get a list of colors either from `colormap`, or from `color`,
91
+ or from `color_type` (if both `colormap` and `color` are None).
92
+
93
+ Parameters
94
+ ----------
95
+ color : str or sequence, optional
96
+ Color(s) to be used for deriving sequence of colors.
97
+ Can be either be a single color (single color string, or sequence of floats
98
+ representing a single color), or a sequence of colors.
99
+ colormap : :py:class:`matplotlib.colors.Colormap`, optional
100
+ Matplotlib colormap.
101
+ When provided, the resulting colors will be derived from the colormap.
102
+ color_type : {"default", "random"}, optional
103
+ Type of colors to derive. Used if provided `color` and `colormap` are None.
104
+ Ignored if either `color` or `colormap`` are not None.
105
+ num_colors : int
106
+ Number of colors to be extracted.
107
+
108
+ Returns
109
+ -------
110
+ list
111
+ List of colors extracted.
112
+
113
+ Warns
114
+ -----
115
+ UserWarning
116
+ If both `colormap` and `color` are provided.
117
+ Parameter `color` will override.
118
+ """
56
119
if color is None and colormap is not None :
57
120
return _get_colors_from_colormap (colormap , num_colors = num_colors )
58
121
elif color is not None :
@@ -115,10 +178,10 @@ def _get_colors_from_color(
115
178
116
179
117
180
def _is_single_color (color : Union [Color , Collection [Color ]]) -> bool :
118
- """Check if `` color` ` is a single color, not a sequence of colors.
181
+ """Check if `color` is a single color, not a sequence of colors.
119
182
120
183
Single color is of these kinds:
121
- - Named color "red"
184
+ - Named color "red", "C0", "firebrick"
122
185
- Alias "g"
123
186
- Sequence of floats, such as (0.1, 0.2, 0.3) or (0.1, 0.2, 0.3, 0.4).
124
187
@@ -167,15 +230,15 @@ def _get_colors_from_color_type(color_type: str, num_colors: int) -> List[Color]
167
230
168
231
169
232
def _get_default_colors (num_colors : int ) -> List [Color ]:
170
- """Get `` num_colors` ` of default colors from matplotlib rc params."""
233
+ """Get `num_colors` of default colors from matplotlib rc params."""
171
234
import matplotlib .pyplot as plt
172
235
173
236
colors = [c ["color" ] for c in plt .rcParams ["axes.prop_cycle" ]]
174
237
return colors [0 :num_colors ]
175
238
176
239
177
240
def _get_random_colors (num_colors : int ) -> List [Color ]:
178
- """Get `` num_colors` ` of random colors."""
241
+ """Get `num_colors` of random colors."""
179
242
return [_random_color (num ) for num in range (num_colors )]
180
243
181
244
@@ -187,9 +250,9 @@ def _random_color(column: int) -> List[float]:
187
250
188
251
189
252
def _is_single_string_color (color : Color ) -> bool :
190
- """Check if `` color`` is a single color.
253
+ """Check if `color` is a single string color.
191
254
192
- Examples of single colors:
255
+ Examples of single string colors:
193
256
- 'r'
194
257
- 'g'
195
258
- 'red'
@@ -205,7 +268,7 @@ def _is_single_string_color(color: Color) -> bool:
205
268
Returns
206
269
-------
207
270
bool
208
- True if `` color` ` looks like a valid color.
271
+ True if `color` looks like a valid color.
209
272
False otherwise.
210
273
"""
211
274
conv = matplotlib .colors .ColorConverter ()
0 commit comments