Search code examples
pandasplotfacetseaborn

seaborn facetgrid pointplot issues


I'm trying to create a seaborn facetgrid with pointplots.

My data is:

DaysIn  GroupUID    OTU_id  value
1   3.A 1   33
1   3.B 1   73.17647059
1   4.A 1   48.12903226
1   4.B 1   39.875
2   3.A 1   23.76470588
2   3.B 1   43.71428571
2   4.A 1   51.73333333
2   4.B 1   42.06896552
3   3.A 1   2
3   3.B 1   10.71428571
3   4.A 1   12.93333333
3   4.B 1   15
4   4.A 1   11.53333333
4   4.B 1   27.86206897
5   3.A 1   1.882352941
5   3.B 1   1.857142857
5   4.A 1   41.9
5   4.B 1   46.28571429
6   4.A 1   58.7
6   4.B 1   101.0344828
7   3.A 1   1.466666667
7   3.B 1   3.428571429
7   4.A 1   74.9
7   4.B 1   102.4137931
8   3.A 1   10.875
8   3.B 1   5.538461538
8   4.A 1   35.5
8   4.B 1   72.33333333
9   3.A 1   21.6
9   3.B 1   16.42857143
10  3.A 1   25.46666667
10  3.B 1   18.85714286
14  3.A 1   29
14  3.B 1   65.8
21  3.A 1   117.1428571
21  3.B 1   104.2142857
28  3.A 1   91.06666667
28  3.B 1   52.78571429
1   3.A 2   27.0625
1   3.B 2   14.23529412
1   4.A 2   32.74193548
1   4.B 2   18.625
2   3.A 2   15
2   3.B 2   23.64285714
2   4.A 2   13.1
2   4.B 2   19.75862069
3   3.A 2   7.6875
3   3.B 2   2.928571429
3   4.A 2   24.46666667
3   4.B 2   29.86206897
4   4.A 2   43.2
4   4.B 2   22.4137931
5   3.A 2   3.117647059
5   3.B 2   4.428571429
5   4.A 2   24.13333333
5   4.B 2   43.35714286
6   4.A 2   31.13333333
6   4.B 2   81.72413793
7   3.A 2   1.333333333
7   3.B 2   4.857142857
7   4.A 2   368.4666667
7   4.B 2   62.34482759
8   3.A 2   0.9375
8   3.B 2   4.384615385
8   4.A 2   0
8   4.B 2   0.333333333
9   3.A 2   0.733333333
9   3.B 2   2.357142857
10  3.A 2   3.4
10  3.B 2   1.357142857
14  3.A 2   4.3
14  3.B 2   0.4
21  3.A 2   2.357142857
21  3.B 2   6.285714286
28  3.A 2   15.93333333
28  3.B 2   9.571428571

The following code will generate the following figure:

    g = sns.FacetGrid(long_df, col='GroupUID', row='OTU_id', sharey=False, sharex=False, margin_titles=True)
    g = g.map(sns.pointplot,'DaysIn','value')
    g.add_legend()

enter image description here

You can see that the figure is just about perfect, except for the "None" title in the bottom right facet.

The following code will generate the following figure:

    g = sns.FacetGrid(long_df, col='GroupUID', hue='OTU_id', sharey=False, sharex=False, margin_titles=True)
    g = g.map(sns.pointplot,'DaysIn','value')
    g.add_legend()

enter image description here

Same issues as above (with the "None") and the legend is not being rendered correctly with color.

Am I missing something? What am I doing wrong?


Solution

  • There are two issues here: the None in the title I cannot reproduce; I suspect you're using an older version of seaborn?

    The legend issue is described here: essentially the pointplot has its own hue argument, so you can change the location which you specify the hue:

    g = sns.FacetGrid(long_df, col='GroupUID', sharey=False, sharex=False, margin_titles=True)
    g = g.map(sns.pointplot,'DaysIn','value', 'OTU_id', palette='deep')
    g.add_legend()
    

    enter image description here

    The other answer also recommends using factorplot, which basically combines FacetGrid and pointplot within one interface:

    g = sns.factorplot(x="DaysIn", y='value', hue="OTU_id",
                       col='GroupUID', data=long_df, sharex=False, sharey=False)
    

    enter image description here

    The result is a bit different, but is better reflective of the missing values in your data.