%matplotlib inline
import seaborn as sns
titanic = sns.load_dataset('titanic')
ax1 = sns.distplot(titanic['fare'], kde=False, bins=15,)
# ax1.set_yscale('log')
for p in ax1.patches:
ax1.annotate(
s=f"{p.get_height():1.0f}",
xy=(p.get_x() + p.get_width() / 2., p.get_height()),
xycoords='data',
ha='center',
va='center',
fontsize=11,
color='black',
xytext=(0,7),
textcoords='offset points',
)
以上代码绘制了泰坦尼克号数据集中
Fare
的直方图,其中每个条形图都使用ax1.annotate
注释其值。问题在于当我想将y轴比例设置为对数刻度时--取消set_yscale
行的注释并运行它,它会抛出一个错误,如下所示:
也许应该更改xycoords参数,但我不太确定要更改为什么。ValueError: 378x84035像素的图像大小太大了。每个方向上必须小于2^16。
我使用的是Python 3.7.2,seaborn版本0.9.0。Matplotlib版本3.0.2,并且我正在使用Inline后端。
ann = ax1.annotate(...); ann.set_in_layout(False)
。如果你写出一个答案,我会接受的。 - Troy