%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',
)
Run Code Online (Sandbox Code Playgroud)
上面的代码绘制了泰坦尼克号数据集的直方图Fare,其中每个条形都使用 注释了其值ax1.annotate。当我想将 y 刻度设置为 logc 时,麻烦就来了——取消注释该set_yscale行并运行它;它抛出一个错误说:
ValueError:378x84035 像素的图像尺寸太大。每个方向都必须小于 2^16。
也许应该更改 xycoords 参数,但我也不太确定要更改什么。
我使用的是Python 3.7.2,seaborn的版本是0.9.0。Matplotlib 版本 3.0.2,我使用的是内联后端。