【问题标题】:Frequency in seaborn histogramsseaborn 直方图中的频率
【发布时间】:2020-07-05 08:43:27
【问题描述】:

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

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

情节看起来像这样:

有什么方法可以描述图中每个 bin 的频率值

PS:我知道我可以使用 numpy histogram 函数获取值

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

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

【问题讨论】:

标签: python seaborn


【解决方案1】:

您可以遍历属于 ax 的补丁,获取它们的位置和高度,并使用这些来创建注释。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-19
    • 2020-09-22
    • 1970-01-01
    • 2015-10-11
    • 1970-01-01
    • 1970-01-01
    • 2013-10-09
    • 2022-10-14
    相关资源
    最近更新 更多