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.

Incorrect legend in lineplot when using numeric values in hue

See original GitHub issue

I believe this is related to #1560 but happens even when style is not used and gives incorrect behaviour with lineplot.

Here’s an example. The legend should have two items, 0.2 and 0.3

import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd

df = pd.DataFrame(
    [
        [0, 0, .2],
        [1, 1, .2],
        [2, 2, .2],
        [0, 0.5, .3],
        [1, 1.5, .3],
        [2, 2.5, .3],
    ],
    columns=['x','y','hue']
)

sns.lineplot(data=df, x='x', y='y', hue='hue')
plt.show()

seaborn_bug_1

Interestingly it works if the hue column contains strings or integers as the categorical value rather than floats. I guess casting that column to a string could be a workaround in the meantime.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:11
  • Comments:9

github_iconTop GitHub Comments

25reactions
arvoelkecommented, Sep 30, 2019

My current work-around is to convert the hue entries into strings that will not be reinterpreted as something else. e.g.,

df["hue"] = ["$%s$" % x for x in df["hue"]]

where the $...$ is a hack (with the side-effect of rendering the number in LaTeX) that prevents:

AttributeError: 'str' object has no attribute 'view'

Other schemes such as "= %s" % x also work.

24reactions
alunawatcommented, Apr 29, 2020

Just set legend=‘full’ in the call to seaborn lineplot

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is this an error in the seaborn.lineplot hue parameter?
This is a known bug of seaborn when the hue can be cast to integers. You could add a prefix to the hue...
Read more >
seaborn.lineplot — seaborn 0.12.1 documentation - PyData |
If “brief”, numeric hue and size variables will be represented with a sample of evenly spaced values. If “full”, every group will get...
Read more >
seaborn.lineplot() method in Python - GeeksforGeeks
Syntax : sns. · Parameters: · x, y: Input data variables; must be numeric. · hue: Grouping variable that will produce lines with...
Read more >
Beautifying the Messy Plots in Python & Solving Common ...
sns.countplot(x = 'method', hue = 'number, data = df2) ... We can override the legend setting using Matplotlib. bbox_to_anchor allows you to ...
Read more >
seaborn.lineplot — seaborn 0.9.0 documentation
If “brief”, numeric hue and size variables will be represented with a sample of evenly spaced ... If False , no legend data...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

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