From 823142e9bf2030ccb4ce259459bd4ab4c5d432be Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Mon, 21 Apr 2025 14:42:23 -0700 Subject: [PATCH] CLN: Use newer numpy random Generator methods in plotting colors --- pandas/plotting/_matplotlib/style.py | 28 ++++++---------------------- 1 file changed, 6 insertions(+), 22 deletions(-) diff --git a/pandas/plotting/_matplotlib/style.py b/pandas/plotting/_matplotlib/style.py index 6e343b176b5eb..7cf63c8621392 100644 --- a/pandas/plotting/_matplotlib/style.py +++ b/pandas/plotting/_matplotlib/style.py @@ -22,8 +22,6 @@ from pandas.core.dtypes.common import is_list_like -import pandas.core.common as com - if TYPE_CHECKING: from matplotlib.colors import Colormap @@ -251,31 +249,17 @@ def _is_floats_color(color: Color | Collection[Color]) -> bool: def _get_colors_from_color_type(color_type: str, num_colors: int) -> list[Color]: """Get colors from user input color type.""" if color_type == "default": - return _get_default_colors(num_colors) + prop_cycle = mpl.rcParams["axes.prop_cycle"] + return [ + c["color"] + for c in itertools.islice(prop_cycle, min(num_colors, len(prop_cycle))) + ] elif color_type == "random": - return _get_random_colors(num_colors) + return np.random.default_rng(num_colors).random((num_colors, 3)).tolist() else: raise ValueError("color_type must be either 'default' or 'random'") -def _get_default_colors(num_colors: int) -> list[Color]: - """Get `num_colors` of default colors from matplotlib rc params.""" - colors = [c["color"] for c in mpl.rcParams["axes.prop_cycle"]] - return colors[0:num_colors] - - -def _get_random_colors(num_colors: int) -> list[Color]: - """Get `num_colors` of random colors.""" - return [_random_color(num) for num in range(num_colors)] - - -def _random_color(column: int) -> list[float]: - """Get a random color represented as a list of length 3""" - # GH17525 use common._random_state to avoid resetting the seed - rs = com.random_state(column) - return rs.rand(3).tolist() # type: ignore[return-value] - - def _is_single_string_color(color: Color) -> bool: """Check if `color` is a single string color.