diff --git a/doc/source/whatsnew/v0.21.0.txt b/doc/source/whatsnew/v0.21.0.txt index e0e0c18052550..da3cf965cb81a 100644 --- a/doc/source/whatsnew/v0.21.0.txt +++ b/doc/source/whatsnew/v0.21.0.txt @@ -614,6 +614,7 @@ Plotting - 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`) - 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`) - Bug in ``Series.plot.bar`` or ``DataFramee.plot.bar`` with ``y`` not respecting user-passed ``color`` (:issue:`16822`) +- Bug causing ``plotting.parallel_coordinates`` to reset the random seed when using random colors (:issue:`17525`) Groupby/Resample/Rolling diff --git a/pandas/plotting/_style.py b/pandas/plotting/_style.py index 8cb4e30e0d91c..bd581c93f1011 100644 --- a/pandas/plotting/_style.py +++ b/pandas/plotting/_style.py @@ -9,7 +9,7 @@ import numpy as np from pandas.core.dtypes.common import is_list_like -from pandas.compat import range, lrange, lmap +from pandas.compat import lrange, lmap import pandas.compat as compat from pandas.plotting._compat import _mpl_ge_2_0_0 @@ -111,11 +111,13 @@ def _get_standard_colors(num_colors=None, colormap=None, color_type='default', if isinstance(colors, compat.string_types): colors = list(colors) elif color_type == 'random': - import random + from pandas.core.common import _random_state def random_color(column): - random.seed(column) - return [random.random() for _ in range(3)] + """ Returns a random color represented as a list of length 3""" + # GH17525 use common._random_state to avoid resetting the seed + rs = _random_state(column) + return rs.rand(3).tolist() colors = lmap(random_color, lrange(num_colors)) else: diff --git a/pandas/tests/plotting/test_misc.py b/pandas/tests/plotting/test_misc.py index c4795ea1e1eca..957369a20f16e 100644 --- a/pandas/tests/plotting/test_misc.py +++ b/pandas/tests/plotting/test_misc.py @@ -284,3 +284,20 @@ def test_subplot_titles(self): title=title[:-1]) title_list = [ax.get_title() for sublist in plot for ax in sublist] assert title_list == title[:3] + [''] + + def test_get_standard_colors_random_seed(self): + # GH17525 + df = DataFrame(np.zeros((10, 10))) + + # Make sure that the random seed isn't reset by _get_standard_colors + plotting.parallel_coordinates(df, 0) + rand1 = random.random() + plotting.parallel_coordinates(df, 0) + rand2 = random.random() + assert rand1 != rand2 + + # Make sure it produces the same colors every time it's called + from pandas.plotting._style import _get_standard_colors + color1 = _get_standard_colors(1, color_type='random') + color2 = _get_standard_colors(1, color_type='random') + assert color1 == color2