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 9 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 in ``plotting._style._get_standard_colors`` causing ``plotting.parallel_coordinates`` to reset the random seed when using random colors (:issue:`17525`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you don't need the first part, a user won't know what you are talking about.



Groupby/Resample/Rolling
Expand Down
8 changes: 5 additions & 3 deletions pandas/plotting/_style.py
Original file line number Diff line number Diff line change
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):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think random provides a context manger, maybe do this like:

rstate = random.getstate()
try:
    random.seed(...)
    return = [random...... ]
finally:
    random.setstate(rstate)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually just use this

In [20]: rs = pandas.core.common._random_state()

In [21]: rs.rand(3)
Out[21]: array([ 0.6786481 ,  0.94231001,  0.03334007])

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()
return rs.rand(3)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return rs.rand(3).tolist() will make this work.


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):
""" For #17525 """
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally, we just add a comment above, not a docstring.

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