Seaborn: Overlay line plot on top of bar chart

2019-07-23 21:28发布

问题:

Here is my dataframe -

    easy_donor  length  count   freq
0   Donor 1 NS  0   15637   0.000188
1   Donor 1 NS  1   144539  0.001737
2   Donor 1 NS  2   129792  0.001560
3   Donor 1 NS  3   143610  0.001726
4   Donor 1 NS  4   189110  0.002273
5   Donor 1 NS  5   474718  0.005706
6   Donor 1 NS  6   741730  0.008916
7   Donor 1 NS  7   908024  0.010915
8   Donor 1 NS  8   1892080 0.022744
9   Donor 1 NS  9   3107487 0.037355
10  Donor 1 NS  10  3789310 0.045551
11  Donor 1 NS  11  6321035 0.075984
12  Donor 1 NS  12  7469065 0.089784
13  Donor 1 NS  13  8493704 0.102101
14  Donor 1 NS  14  9633218 0.115799
15  Donor 1 NS  15  9008967 0.108295
16  Donor 1 NS  16  7682835 0.092354
17  Donor 1 NS  17  6669647 0.080175
18  Donor 1 NS  18  5199193 0.062499
19  Donor 1 NS  19  3809540 0.045794

I'm plotting it like this.

import seaborn as sns
g = sns.factorplot(y='freq',
                   x='length',
                   data=collection_lengths,
                   col='easy_donor',
                   hue='easy_donor',
                   kind='bar')
g.set(xticks=range(2,31,3),xticklabels=range(2,31,3))
g.set_titles("")
g.set_axis_labels("HCDR3 Length","Frequency")
g.set_xticklabels(rotation=30)

Is it possible to overlay the connecting line over the bar graph in a factorplot like this?

EDIT -

What I mean by overlay is to have the line plot like this:

On top of the actual barchart (of course I would remove the points and just have the line).

回答1:

You might find this previous question useful which points to this seaborn tutorial.

I think this snippet will get you most of the way,

g = sns.FacetGrid(collection_lengths, col="easy_donor", hue='easy_donor')
g = g.map(sns.barplot, 'length', 'freq')
g = g.map(sns.pointplot, 'length', 'freq')
g.set(xticks=range(2,31,3),xticklabels=range(2,31,3))
g.set_axis_labels("HCDR3 Length","Frequency")
g.set_xticklabels(rotation=30)