distplot 中每个 bin 的计数
Counts of each bin in a distplot
有什么方法可以得到sns.distplot中每个bin的计数吗?例如,
sns.distplot(df.intervalL, kde=False, bins=[0,25,50,75,100], color='navy',rug=False, norm_hist=False)
我想获取每个容器中的计数。
谢谢
sns.distplot
returns 创建绘图的 ax
。在 ax.containers[0]
中,条形可作为矩形访问,您可以调查它们的高度。
请注意,在 Seaborn 0.11 中,sns.distplot
已被 sns.histplot
取代。 (下面的代码仍然适用于 sns.distplot
,显示该函数已被弃用的警告。)
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
data = np.random.randn(100, 10).cumsum(axis=1).ravel()
data -= data.min()
data *= 100 / data.max()
bins = [0,25,50,75,100]
ax = sns.histplot(data, kde=False, bins=bins, color='navy')
for bar, b0, b1 in zip(ax.containers[0], bins[:-1], bins[1:]):
print(f'{b0:3d} - {b1:3d}: {bar.get_height():4.0f}')
输出:
0 - 25: 57
25 - 50: 513
50 - 75: 382
75 - 100: 48
如果你只想要没有绘图的值,你也可以只调用 counts, _ = np.histogram(data, bins=bins)
(docs).
有什么方法可以得到sns.distplot中每个bin的计数吗?例如,
sns.distplot(df.intervalL, kde=False, bins=[0,25,50,75,100], color='navy',rug=False, norm_hist=False)
我想获取每个容器中的计数。 谢谢
sns.distplot
returns 创建绘图的 ax
。在 ax.containers[0]
中,条形可作为矩形访问,您可以调查它们的高度。
请注意,在 Seaborn 0.11 中,sns.distplot
已被 sns.histplot
取代。 (下面的代码仍然适用于 sns.distplot
,显示该函数已被弃用的警告。)
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
data = np.random.randn(100, 10).cumsum(axis=1).ravel()
data -= data.min()
data *= 100 / data.max()
bins = [0,25,50,75,100]
ax = sns.histplot(data, kde=False, bins=bins, color='navy')
for bar, b0, b1 in zip(ax.containers[0], bins[:-1], bins[1:]):
print(f'{b0:3d} - {b1:3d}: {bar.get_height():4.0f}')
输出:
0 - 25: 57
25 - 50: 513
50 - 75: 382
75 - 100: 48
如果你只想要没有绘图的值,你也可以只调用 counts, _ = np.histogram(data, bins=bins)
(docs).