python - seaborn 分布图为每个直方图 bin 的计数添加标签
问题描述
如何让 seaborn 为包含每个 bin 的元素数量的分布图添加标签?
import seaborn as sns, numpy as np
sns.set(); np.random.seed(0)
x = np.random.randn(100)
ax = sns.distplot(x)
解决方案
一个粗略的解决方案可能是:
import seaborn as sns, numpy as np
import matplotlib.pyplot as plt
# add the following line if working on jupyter notebook
%matplotlib inline
sns.set()
np.random.seed(0)
x = np.random.randn(100)
ax = sns.distplot(x)
s = 0
for p in ax.patches:
s+= p.get_height()
for p in ax.patches:
ax.text(p.get_x() + p.get_width()/2.,
p.get_height(),
'{}'.format(int(p.get_height()*100/s)),
fontsize=14,
color='red',
ha='center',
va='bottom')
plt.show()
你得到:
推荐阅读
- angular - Jasmine/Angular:测试 void EventEmitter onInit 的接收?
- android - Firebase 主题订阅的实例 ID
- visual-studio - Visual-Studio 窗口的放大显示
- sql - SQL 显示带有子标题的产品的父标题
- rabbitmq - Spring AMQP(RabbitMQ)抛出通道关闭错误
- sas - 在 SAS 中使用“set”两次组合数据集
- node.js - Node.js 上突然出现 400 Bad Request 错误
- php - PHP在单行中获取所有事件
- php - 无法从数据库中回显用户数据 - 说“属性 [id] 不存在”
- ios - 获取我使用 Interface Builder 添加的 UIBarButtonItems