如何在 matplotlib/seaborn 中缩放直方图条的高度?
How to scale histogram bar heights in matplotlib / seaborn?
我的数据集是一个大小为 (m, 1) 的 numpy 数组,我需要 2 个图:a) 对整个数据进行归一化(概率)之一 b) 数据集的一个子集保持与以前相同的归一化.
问题在于,在情况 b) 中,matplotlib 和 seaborn 提供的规范化选项只能“看到”子集,因此它们无法基于整个数据进行规范化。
基本上我想做的是:
bar_height = bar_count / m
示例数据:
array([[-0.00996642],
[ 0.00407526],
[ 0.00547561],
...,
[ 0.05205999],
[ 0.00224144],
[ 0.01201942]])
您可以使用 np.histogram()
to calculate the histogram and then draw the bars with plt.bar()
:
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
sns.set()
m = 200
samples = np.random.rand(1000)
hist_values, bin_edges = np.histogram(samples)
plt.bar(x=bin_edges[:-1], height=hist_values / m, width=np.diff(bin_edges), align='edge')
plt.show()
我的数据集是一个大小为 (m, 1) 的 numpy 数组,我需要 2 个图:a) 对整个数据进行归一化(概率)之一 b) 数据集的一个子集保持与以前相同的归一化.
问题在于,在情况 b) 中,matplotlib 和 seaborn 提供的规范化选项只能“看到”子集,因此它们无法基于整个数据进行规范化。
基本上我想做的是:
bar_height = bar_count / m
示例数据:
array([[-0.00996642],
[ 0.00407526],
[ 0.00547561],
...,
[ 0.05205999],
[ 0.00224144],
[ 0.01201942]])
您可以使用 np.histogram()
to calculate the histogram and then draw the bars with plt.bar()
:
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
sns.set()
m = 200
samples = np.random.rand(1000)
hist_values, bin_edges = np.histogram(samples)
plt.bar(x=bin_edges[:-1], height=hist_values / m, width=np.diff(bin_edges), align='edge')
plt.show()