Skip to content

BUG: GH17525 Function _get_standard_colors resets global random seed #17730

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Oct 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.21.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 6 additions & 4 deletions pandas/plotting/_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand Down
17 changes: 17 additions & 0 deletions pandas/tests/plotting/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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