海洋直方图中的频率

时间:2020-07-05 08:43:27

标签: python seaborn

我有一个二手车数据集。我已经绘制了一个直方图,用于按年龄(以月为单位)划分汽车的数量。

sns.distplot(df['Age'],kde=False,bins=6)

情节看起来像这样:

enter image description here

有什么办法可以描述绘图中每个仓位的频率值

PS:我知道我可以使用numpy直方图函数来获取值

np.histogram(df['Age'],bins=6)

我基本上希望我的情节看起来像这样:

enter image description here

1 个答案:

答案 0 :(得分:0)

您可以遍历斧头的补丁,获取它们的位置和高度,并使用它们创建注释。

import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
import pandas as pd

sns.set_style()
df = pd.DataFrame({'Age': np.random.triangular(1, 80, 80, 1000).astype(np.int)})
ax = sns.distplot(df['Age'], kde=False, bins=6)

for p in ax.patches:
    ax.annotate(f'{p.get_height():.0f}\n',
                (p.get_x() + p.get_width() / 2, p.get_height()), ha='center', va='center', color='crimson')
plt.show()

example plot

相关问题