在seaborn.stipplot
中是否可以注释每个点?我的数据在pandas
中。我知道matplotlib
中的annotate可以实现,但在这种情况下我还没有找到它的用法。
在seaborn.stipplot
中是否可以注释每个点?我的数据在pandas
中。我知道matplotlib
中的annotate可以实现,但在这种情况下我还没有找到它的用法。
seaborn
是一个基于 matplotlib
的高级 API,因此将返回的 axes
分配给别名,然后使用该对象与 .annotate
一起使用。
这个例子改编自 seaborn
文档 这里。
在 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')