Skip to content

Commit 07039a6

Browse files
authored
CLN: Use newer numpy random Generator methods in plotting colors (#61332)
1 parent cf6de58 commit 07039a6

File tree

1 file changed

+6
-22
lines changed

1 file changed

+6
-22
lines changed

pandas/plotting/_matplotlib/style.py

+6-22
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222

2323
from pandas.core.dtypes.common import is_list_like
2424

25-
import pandas.core.common as com
26-
2725
if TYPE_CHECKING:
2826
from matplotlib.colors import Colormap
2927

@@ -251,31 +249,17 @@ def _is_floats_color(color: Color | Collection[Color]) -> bool:
251249
def _get_colors_from_color_type(color_type: str, num_colors: int) -> list[Color]:
252250
"""Get colors from user input color type."""
253251
if color_type == "default":
254-
return _get_default_colors(num_colors)
252+
prop_cycle = mpl.rcParams["axes.prop_cycle"]
253+
return [
254+
c["color"]
255+
for c in itertools.islice(prop_cycle, min(num_colors, len(prop_cycle)))
256+
]
255257
elif color_type == "random":
256-
return _get_random_colors(num_colors)
258+
return np.random.default_rng(num_colors).random((num_colors, 3)).tolist()
257259
else:
258260
raise ValueError("color_type must be either 'default' or 'random'")
259261

260262

261-
def _get_default_colors(num_colors: int) -> list[Color]:
262-
"""Get `num_colors` of default colors from matplotlib rc params."""
263-
colors = [c["color"] for c in mpl.rcParams["axes.prop_cycle"]]
264-
return colors[0:num_colors]
265-
266-
267-
def _get_random_colors(num_colors: int) -> list[Color]:
268-
"""Get `num_colors` of random colors."""
269-
return [_random_color(num) for num in range(num_colors)]
270-
271-
272-
def _random_color(column: int) -> list[float]:
273-
"""Get a random color represented as a list of length 3"""
274-
# GH17525 use common._random_state to avoid resetting the seed
275-
rs = com.random_state(column)
276-
return rs.rand(3).tolist()
277-
278-
279263
def _is_single_string_color(color: Color) -> bool:
280264
"""Check if `color` is a single string color.
281265

0 commit comments

Comments
 (0)