-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: allow numpy.array as c values to scatterplot #8929
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
Conversation
@@ -2500,6 +2500,31 @@ def is_list_like(arg): | |||
not isinstance(arg, compat.string_and_binary_types)) | |||
|
|||
|
|||
def is_hashy(arg): |
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.
looks pretty good to me, though I would call this is_hashable
rather than is_hashy
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.
yep is_hashable is better
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 was thinking that originally, I felt uncomfortable because of the surprising implementation.
Would is_safely_hashable
be good maybe?
Thanks!
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.
Personally, I would not expect is_hashable
to use collections.Hashable
(if you wanted that, you could just do the isinstance check). So this implementation does not surprise me, at least. Also, I'm pretty sure the Python docs explicitly state that __hash__
is supposed to be cheap to evaluate.
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.
Ok cool, have rebased and renamed to is_hashable
, thanks for explaining
@@ -145,6 +145,8 @@ Bug Fixes | |||
- BUG: Option context applies on __enter__ (:issue:`8514`) | |||
|
|||
|
|||
- Bug where plot with kind=scatter fails when checking if an array is in the DataFrame (:issue:`8852`) |
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.
say DataFrame.plot(kind='scatter')
pls squash, otherwise looks ok |
# | ||
df = DataFrame({'A': [1, 2], 'B': [3, 4]}) | ||
c = np.array([1, 0]) | ||
df.plot(kind='scatter', x='A', y='B', c=c) |
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.
Can you check that the colors are correct? Get back the result from plot with ax = df.plot(kind='scatter', x='A', y='B', c=c)
and get the collections
attribute, and check ax.collections[0].get_facecolor()
against the expected array([[ 1., 1., 1., 1.], [ 0., 0., 0., 1.]])
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.
@TomAugspurger Is that the expected color? I think the [1, 0] are interpreted as numeric values that are scaled to the default colormap, not as rgb colors? (when you do c=[[1,1,1],[0,0,0]]
matplotlib does something different)
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.
That's what I get for fig, ax = plt.subplots(); ax.scatter([0, 0], [1, 1], c=[0, 1])
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.
c can be a single color format string, or a sequence of color specifications of length N, or a sequence of N numbers to be mapped to colors using the cmap and norm specified via kwargs (see below). Note that c should not be a single numeric RGB or RGBA sequence because that is indistinguishable from an array of values to be colormapped. c can be a 2-D array in which the rows are RGB or RGBA, however.
from http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.scatter
We should handle the same if possible.
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.
Weird that the order is flipped if you do
ax.scatter([0, 0], [1, 1], c=[0, 1])
vs ax.scatter([0, 0], [1, 1], c=[[0, 0, 0, 1], [1, 1, 1, 1]])
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.
Hmm, because I did the same and got colors and not black/white. You don't have a custom matplotlib defaults that sets cmap at grayscale? (maybe seaborn does that?)
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 have seaborn imported in my IPython profile.
I'm fine with this being two commits since it separates the addition of Could you add a test or two for |
@aevri pls add test for |
I've updated with these changes:
Was considering how to test the face colors of the case where we supply a colormap, don't know how to query the colormap ahead of time though. Cheers! |
@aevri By the way, David (@cournape) fixed it in numpy today: numpy/numpy#5326 And the tests are failing on python 3 (and on numpy master, because of Davids fix) |
14fac02
to
9881d87
Compare
Have updated:
Cheers! |
Some types will pass a test against collections.Hashable but fail when they are actually hashed with hash(). Introduce a new predicate to help handle these types. Will use this in subsequent commit. Test Plan: In addition to running tests, run the doctest $ nosetests pandas/core/common.py:is_hashable --with-doc -v
Ensure that we can pass an np.array as 'c' straight through to matplotlib, this functionality was accidentally removed previously. Add tests. Closes pandas-dev#8852
Rebased again for new merge conflicts Afaik all points are addressed and this is ready to land, please let me know if there's something I've missed :O) |
Looks OK for me! Lets wait until travis is green |
@aevri thanks for the fix! |
Yay! Thanks everyone :) |
Ensure that we can pass an np.array as 'c' straight through to
matplotlib, this functionality was accidentally removed previously.
Add tests.
Closes #8852