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 4 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`` resetting the random seed when generating 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.

reference the user visibile issue (e.g. parallel coordinates)?



Groupby/Resample/Rolling
Expand Down
5 changes: 4 additions & 1 deletion pandas/plotting/_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,11 @@ def _get_standard_colors(num_colors=None, colormap=None, color_type='default',
import random

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])

rstate = random.getstate() # to avoid resetting the seed
Copy link
Member

Choose a reason for hiding this comment

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

Point to the issue number.

Copy link
Member

@gfyoung gfyoung Oct 1, 2017

Choose a reason for hiding this comment

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

Also, since you're changing the behavior, let's document it. A brief doc-string would be good here for future developers.

random.seed(column)
return [random.random() for _ in range(3)]
color = [random.random() for _ in range(3)]
random.setstate(rstate)
return color

colors = lmap(random_color, lrange(num_colors))
else:
Expand Down