seaborn facetgrid: when hue value doesn't appear in all graphs the legend is misannotated

I've also opened an issue in the seaborn git repo here. However, instead of a bug, it's quite possible that instead I'm making some basic error - but I haven't figured it out yet...

Question: The same color is assigned to two different values in the legend. How to prevent this from happening as I scale up to more graphs that only contain a subset of the values assigned to the 'hue'?

The code to reproduce:

import numpy as np
import pandas as pd

import seaborn as sns
import matplotlib.pyplot as plt


sna = pd.DataFrame(
    {
        'x' : np.tile(range(10), 5), 
        'y' : list(range(10)) + list(range(2,12)) + list(range(4, 14)) + list(range(6, 16)) + list(range(8, 18)),
        'id_for_hue' : ['foo']*10 + ['bar']*10 + ['baz']*10 + ['bar']*10 + ['baz']*10,
        'id_for_graph' : ['a']*30 + ['b']*20
    }                                
)

g = sns.FacetGrid(sna, col='id_for_graph')
g.map_dataframe(
    sns.lineplot,
    x = 'x',
    y = 'y',
    hue = 'id_for_hue'
)

g.add_legend()

enter image description here

On the other hand, if I sort so that the values from the second plot appear at the top of the dataframe, the problem is resolved.

g = sns.FacetGrid(sna.sort_values(['id_for_hue']), col='id_for_graph')
g.map_dataframe(
    sns.lineplot,
    x = 'x',
    y = 'y',
    hue = 'id_for_hue'
)

g.add_legend()

enter image description here

Versions:

  • seaborn: 0.11.2
  • matplotlib: 3.5.2
  • pandas: 1.3.4
  • numpy: 1.21.6
  • python: 3.7.6


Comments

Popular posts from this blog

Today Walkin 14th-Sept

Spring Elasticsearch Operations

Hibernate Search - Elasticsearch with JSON manipulation