-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
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
BUG: GH17525 Function _get_standard_colors resets global random seed #17730
Conversation
Codecov Report
@@ Coverage Diff @@
## master #17730 +/- ##
==========================================
- Coverage 91.27% 91.23% -0.04%
==========================================
Files 163 163
Lines 49766 49859 +93
==========================================
+ Hits 45422 45490 +68
- Misses 4344 4369 +25
Continue to review full report at Codecov.
|
pandas/plotting/_style.py
Outdated
@@ -114,8 +114,11 @@ def _get_standard_colors(num_colors=None, colormap=None, color_type='default', | |||
import random | |||
|
|||
def random_color(column): | |||
rstate = random.getstate() # to avoid resetting the seed |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
@cmazzullo : Also, you're definitely going to need tests. |
Hello @cmazzullo! Thanks for updating the PR. Cheers ! There are no PEP8 issues in this Pull Request. 🍻 Comment last updated on October 03, 2017 at 08:06 Hours UTC |
Okay, just added one. |
pandas/plotting/_style.py
Outdated
@@ -114,8 +114,13 @@ def _get_standard_colors(num_colors=None, colormap=None, color_type='default', | |||
import random | |||
|
|||
def random_color(column): |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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])
doc/source/whatsnew/v0.21.0.txt
Outdated
@@ -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`) |
There was a problem hiding this comment.
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)?
pandas/tests/plotting/test_misc.py
Outdated
@@ -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 """ |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm with a minor comment. have't looked why its failing. ping on green.
doc/source/whatsnew/v0.21.0.txt
Outdated
@@ -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`) |
There was a problem hiding this comment.
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.
pandas/plotting/_style.py
Outdated
""" 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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm. ping on green.
@cmazzullo Thanks! |
Sure thing |
git diff upstream/master -u -- "*.py" | flake8 --diff