Reputation: 17676
How can I add a point count in the point plot in seaborn?
df = pd.DataFrame({'group':[0,1,1],'percentage':[0.5, .3,.7],'value_group':[1,2,3], 'count':[1, 10, 20]})
df['value_group'] = pd.qcut(df.value_group, 2)
group percentage value_group count
0 0 0.5 1 1
1 1 0.3 2 10
2 1 0.7 3 20
With
sns.pointplot(x="value_group", y="percentage", hue="group", data=df)
I get: But instead I would want:
How can this be achieved in seaborn?
They are similar, but not the same. My value_group is obtained using pd.qcut
and the code referenced i.e. cannot handle these.
python Seaborn - annotations with pointplot
import matplotlib.pyplot as plt import seaborn as sns tips = sns.load_dataset("tips") ax = sns.pointplot(x="time", y="total_bill", hue="smoker", data=tips) for c in ax.collections: for of in c.get_offsets(): ax.annotate("Label", of) plt.show()
looks interesting, but so far I do not yet know how to match the right labels/indices with my counts.
Upvotes: 1
Views: 1235
Reputation: 39042
The problem was bit nontrivial that I was thinking. Since you have used hue="group"
, there are two groups in the plot and therefore, ax.collections
has a length of 2. So to have the annotations in the correct order, I used a tacking index j
.
You can zip the offsets and the DataFrame values you want to show, and annotate them using a for loop as
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()
df = pd.DataFrame({'group':[0,1,1],'percentage':[0.5, .3,.7],'value_group':[1,2,3], 'count':[1, 10, 20]})
ax = sns.pointplot(x="value_group", y="percentage", hue="group", data=df)
j = 0 # <--- Index to keep rack of values
values = df['count'].values # <--- store the values in a variable for easy access
for c in ax.collections:
for i, of in zip(range(len(c.get_offsets())), c.get_offsets()):
ax.annotate(values[j], of, color='red', fontsize=24)
j += 1
ax.legend(loc=(0.8, 0.1))
plt.show()
Upvotes: 2