Skip to content

Commit 2e2093e

Browse files
cmazzullojorisvandenbossche
authored andcommitted
BUG: GH17525 Function _get_standard_colors resets global random seed (pandas-dev#17730)
1 parent f797408 commit 2e2093e

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

doc/source/whatsnew/v0.21.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,7 @@ Plotting
728728
- Line plots no longer assume monotonic x data when calculating xlims, they show the entire lines now even for unsorted x data. (:issue:`11310`, :issue:`11471`)
729729
- With matplotlib 2.0.0 and above, calculation of x limits for line plots is left to matplotlib, so that its new default settings are applied. (:issue:`15495`)
730730
- Bug in ``Series.plot.bar`` or ``DataFramee.plot.bar`` with ``y`` not respecting user-passed ``color`` (:issue:`16822`)
731+
- Bug causing ``plotting.parallel_coordinates`` to reset the random seed when using random colors (:issue:`17525`)
731732

732733

733734
Groupby/Resample/Rolling

pandas/plotting/_style.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import numpy as np
1010

1111
from pandas.core.dtypes.common import is_list_like
12-
from pandas.compat import range, lrange, lmap
12+
from pandas.compat import lrange, lmap
1313
import pandas.compat as compat
1414
from pandas.plotting._compat import _mpl_ge_2_0_0
1515

@@ -44,11 +44,13 @@ def _get_standard_colors(num_colors=None, colormap=None, color_type='default',
4444
if isinstance(colors, compat.string_types):
4545
colors = list(colors)
4646
elif color_type == 'random':
47-
import random
47+
from pandas.core.common import _random_state
4848

4949
def random_color(column):
50-
random.seed(column)
51-
return [random.random() for _ in range(3)]
50+
""" Returns a random color represented as a list of length 3"""
51+
# GH17525 use common._random_state to avoid resetting the seed
52+
rs = _random_state(column)
53+
return rs.rand(3).tolist()
5254

5355
colors = lmap(random_color, lrange(num_colors))
5456
else:

pandas/tests/plotting/test_misc.py

+17
Original file line numberDiff line numberDiff line change
@@ -284,3 +284,20 @@ def test_subplot_titles(self):
284284
title=title[:-1])
285285
title_list = [ax.get_title() for sublist in plot for ax in sublist]
286286
assert title_list == title[:3] + ['']
287+
288+
def test_get_standard_colors_random_seed(self):
289+
# GH17525
290+
df = DataFrame(np.zeros((10, 10)))
291+
292+
# Make sure that the random seed isn't reset by _get_standard_colors
293+
plotting.parallel_coordinates(df, 0)
294+
rand1 = random.random()
295+
plotting.parallel_coordinates(df, 0)
296+
rand2 = random.random()
297+
assert rand1 != rand2
298+
299+
# Make sure it produces the same colors every time it's called
300+
from pandas.plotting._style import _get_standard_colors
301+
color1 = _get_standard_colors(1, color_type='random')
302+
color2 = _get_standard_colors(1, color_type='random')
303+
assert color1 == color2

0 commit comments

Comments
 (0)