且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将计数添加到直方图?

更新时间:2023-11-30 23:20:34

您可以尝试执行以下操作:

You can try something like this:

hist返回计数,垃圾箱和补丁. patches是矩形的列表.然后,您可以使用面片矩形的计数和坐标来注释轴.

hist returns counts, bins and patches. patches is a list of rectangles. Then you can annotate the axis using the count and coordinates of the patch rectangles.

import numpy as np
import matplotlib.pyplot as plt

# generate some random data
x = np.random.randn(10000)
x = x * 100
x = x.astype(np.int)

# plot the histogram of the data
bins = np.arange(-300,300,20)
fig = plt.figure(figsize=(15,4))
ax = plt.gca()
counts, _, patches = ax.hist(x, bins=bins,edgecolor='r')
for count, patch in zip(counts,patches):
    ax.annotate(str(int(count)), xy=(patch.get_x(), patch.get_height()))
plt.show()