relplot error when using str as hue
See original GitHub issueWhen 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:
- Created 5 years ago
- Reactions:16
- Comments:22 (6 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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
sns.lineplot(data=df, x='x', y='y', hue='id', palette=sns.color_palette("Set1", 16))
sns.scatterplot(data=df, x='x', y='y', hue='id', palette=sns.color_palette("Set1", 16))
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;