amc
amc

Reputation: 833

Annotate points in a stripplot

Is it possible to annotate each point in a seaborn.stipplot? My data is in pandas. I realize that annotate works for matplotlib, but I have not found it to work in this case.

Upvotes: 3

Views: 3693

Answers (1)

aorr
aorr

Reputation: 966

seaborn is a high-level API for matplotlib, so assign the returned axes to an alias, and then use the object with .annotate.

This example is adapted from the seaborn docs here.

Tested in python 3.11.2, pandas 2.0.0, matplotlib 3.7.1, seaborn 0.12.2

import seaborn as sns

tips = sns.load_dataset("tips")
sat_mean = tips.loc[tips['day'] == 'Sat']['total_bill'].mean()

ax = sns.stripplot(x="day", y="total_bill", data=tips)

ax.annotate("Saturday\nMean",
            xy=(2, sat_mean), xycoords='data',
            xytext=(.5, .5), textcoords='axes fraction',
            horizontalalignment="center",
            arrowprops=dict(arrowstyle="->",
                            connectionstyle="arc3"),
            bbox=dict(boxstyle="round", fc="w"),
            )

# ax.get_figure().savefig('tips_annotation.png')

enter image description here

Upvotes: 8

Related Questions