Skip to content
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

Scatter Points with Incorrect Hue #840

Closed
jthurbs opened this issue Jan 26, 2016 · 12 comments
Closed

Scatter Points with Incorrect Hue #840

jthurbs opened this issue Jan 26, 2016 · 12 comments

Comments

@jthurbs
Copy link

jthurbs commented Jan 26, 2016

I have observed an issue with hues on FacetGrids displaying scatter plots. Some of the plotted points are the incorrect color and yet appear to be represented correctly in the legend. I was able to reproduce this in the following snippet. In the generated plot, the lower left axes includes points whose marker face color is black and some that are white. I originally discovered this phenomena in a larger 3 row x n column FacetGrid with a larger number of hues. In this case, all the axes on the second row had incorrect marker face colors (either black or white).

A second observation is that the legend is overlaid on the axes instead of being displayed to the right of the last column of axes.

import matplotlib.pyplot as plt
import seaborn as sns

tips = sns.load_dataset('tips')
g = sns.FacetGrid(tips, col="time", row="smoker", hue = 'size') # Create Grid
g = (g.map(plt.scatter, "total_bill", "tip")).add_legend()
plt.show()

Image of Issue (mpl 1.5.1)
issue - 1 5 1 marked up

matplotlib version --> 1.5.1
seaborn version --> 0.7.0
matplotlib backend --> TkAgg
python --> 2.7.10
os --> windows 7

It appears that the issue does not occur in matplotlib version 1.4.3
Works in mpl 1.4.3
no issue - 1 4 3

Huge fan of seaborn by the way. Thanks so much for all the work you put into it.

@phobson
Copy link
Contributor

phobson commented Mar 23, 2016

just a note about this:
what's probably happening is matplotlib is interpreting the (r, b, g) tuple as a sequence of grayscale values since there's something else where len(x) = 3

@mwaskom
Copy link
Owner

mwaskom commented Mar 23, 2016

Yeah this was a recent change in matplotlib. I fixed some functions here but forgot about regplot. It's a pain because most of the functions have a number of tests assuming an rgb representation so that conversion to hex has to happen late and with care.

@mwaskom
Copy link
Owner

mwaskom commented Apr 25, 2016

This particular case of the issues with plt.scatter's interpretation of the c parameter ends up being a little tricky because FacetGrid tries to be very general and agnostic to the functions that are passed into it. In other seaborn functions this issue is handled by forcing colors to hex representation before they are passed to plt.scatter, but doing that in FacetGrid could cause issues for other applications and I would like to avoid lots of messy special case code.

Going to punt on changing anything right now to address this, but if you are running into this problem, you can always do, e.g. sns.FacetGrid(..., palette=sns.color_palette("deep", n_colors).as_hex()).

@jonsedar
Copy link

jonsedar commented May 4, 2016

Aha, was banging my head against this for a few hours, so it's nice to at least have the issue confirmed! It's certainly an edge case...

@mwaskom your suggested fix doesn't seem to work for me, so I manually crafted the legend using this link for help http://stackoverflow.com/questions/9262307/matplotlib-legend-with-multiple-different-markers-with-one-label

I can post code if anyone's interested?

@mwaskom
Copy link
Owner

mwaskom commented May 4, 2016

Another workaround would be to use plt.plot with marker="o" and linestyle="", which will also produce a scatterplot.

@unode
Copy link

unode commented Apr 7, 2017

Likely related. I hit a ValueError while trying out the example from #209 (comment)

The exception ValueError: Invalid RGBA argument: 0.29803921568627451 is raised by matplotlib/colors.py.

Versions:
  python-3.6.1
  matplotlib-2.0.0
  seaborn-0.7.1

@mwaskom
Copy link
Owner

mwaskom commented Oct 4, 2017

I believe this may be fixed in matplotlib 2.1 (potentially "by accident" matplotlib/matplotlib#8658).

@mwaskom
Copy link
Owner

mwaskom commented Oct 24, 2017

Closing as fixed in matplotlib. Ping if that turns out not to be the case.

@mwaskom mwaskom closed this as completed Oct 24, 2017
@blsymens
Copy link

blsymens commented Apr 4, 2020

I'm using matplotlib in google colab. Seems that still something messed up with my colours having only 4 records in a given category. Not sure how to solve this.

My code:
sns.pairplot(data,hue='Die plate',corner = True)

I expect to get just two colours. Also in legend I don't seem to get it right. Hopefully, someone could give me some advice. Cheers!

Output:
image

@phobson
Copy link
Contributor

phobson commented Apr 7, 2020

@blsymens it's hard to say what's going on without knowing what versions of python, matplotlib, seaborn you're using and what your data look like.

@blsymens
Copy link

blsymens commented Apr 10, 2020

The versions I use are the following:

  • Python version: 3.6
  • Numpy version:1.18.2
  • Pandas version: 1.0.3
  • Seaborn version: 0.10.0

This is a sample of some simulated data you can use.
DATA.TXT

Help is very much appreciated

@MaozGelbart
Copy link
Contributor

@blsymens this stems from having a group with four points only and matplotlib=3.2 (see #1083 ). Meanwhile try downgrading matplotlib to 3.1.2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants