在条状图中标注点

问题描述 投票:0回答:1

是否可以在

seaborn.stipplot
中标注每个点?我的数据在
pandas
。我意识到注释适用于
matplotlib
,但我没有发现它在这种情况下有效。

pandas matplotlib seaborn annotate stripplot
1个回答
8
投票

seaborn
matplotlib
的高级 API,因此将返回的
axes
分配给一个别名,然后使用带有
.annotate
的对象。

这个例子改编自

seaborn
文档here.

测试于

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')

© www.soinside.com 2019 - 2024. All rights reserved.