question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

relplot error when using str as hue

See original GitHub issue

When I am trying to use a string or categorical column within a dataframe as hue within a relplot we get the following error: AttributeError: 'str' object has no attribute 'view'

Minimal example:

test_df = pd.DataFrame(
[{"a": 0.0, "b": 1.0, "c": "1", "d": "1"},
 {"a": 0.0, "b": 1.0, "c": "2", "d": "2"}]
)
g = sns.relplot(x="a", y="b", col="c",  hue="d", data=test_df)

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:16
  • Comments:22 (6 by maintainers)

github_iconTop GitHub Comments

29reactions
teckwanikarancommented, Apr 11, 2019

This is my first time ever commenting on an open issue, so please be kind if I am wrong!

I was facing similar issues, till I referred to this post on stackoverflow

  • Problem: I have about 16 unique numerical str values as id (like: ‘215’ ‘112233’, …)
  • The fix: Use your palette size to be same as the unique numerical str values.
  • example: sns.lineplot(data=df, x='x', y='y', hue='id', palette=sns.color_palette("Set1", 16))
  • or sns.scatterplot(data=df, x='x', y='y', hue='id', palette=sns.color_palette("Set1", 16))
12reactions
mwaskomcommented, Jul 27, 2018

I think the problem here is that seaborn is duck typing whether the hue variable is categorical or numeric based on whether it can be converted to float without erroring, but then using the original data for getting the colors. Passing numerics as strings is kind of a weird corner case, but it shouldn’t be impossible. There’s a similar issue in PairGrid (#1347) and so a good general solution is needed.

I think in the meantime, a workaround would be to provide explicit colors values to the palette (either as a list or dictionary) which should skip the numeric color-mapping code;

test_df = pd.DataFrame(
[{"a": 0.0, "b": 1.0, "c": "1", "d": "1"},
 {"a": 0.0, "b": 1.0, "c": "2", "d": "2"}]
)
g = sns.relplot(x="a", y="b", col="c",  hue="d", palette=["r", "b"], data=test_df)
Read more comments on GitHub >

github_iconTop Results From Across the Web

relplot hue unable to parse strings it thinks are numeric
I've tried casting that column to a string with df = df.astype({"children": "string"}) , but it didn't work. Any help?
Read more >
seaborn.relplot — seaborn 0.12.1 documentation - PyData |
Method for choosing the colors to use when mapping the hue semantic. String values are passed to color_palette() . List or dict values...
Read more >
Error Bars not displaying Seaborn Relplot-Pandas,Python
Each datapoint is separated by hue , so there are no error bars because no data is being combined. Remove hue='Patient ID' to...
Read more >
Master a third of Seaborn: Statistical plotting with relplot()
Learn a third of Seaborn by learning the `relplot()` function. ... Pro Tip: Use hue and style parameters together to give your plots...
Read more >
Relational plots in Seaborn - Part II - GeeksforGeeks
Though both these plots can be drawn using relplot(), seaborn also have ... Colors to use for the different levels of the hue...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found