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

时间:2019-11-22 12:46:20

标签: python-3.x matplotlib histogram

我想将直方图的计数数据添加到matplotlib中的绘图中。这是我的数据;

import matplotlib.pyplot as plt
data = [['B', 1], ['C', 2], ['A', 3],['A', 4], ['B', 5], ['B', 6],['A', 7], ['B', 8], ['C', 9],['D',10]] 
df = pd.DataFrame(data, columns = ['Name', 'Count']) 
plt.hist(df['Name'])
plt.show()

结果是这样的; result1

我尝试使用plt.textvalue_counts(),但是它们的排序方式有所不同...

import matplotlib.pyplot as plt
data = [['B', 1], ['C', 2], ['A', 3],['A', 4], ['B', 5], ['B', 6],['A', 7], ['B', 8], ['C', 9],['D',10]] 
df = pd.DataFrame(data, columns = ['Name', 'Count']) 

xvals = np.arange(len(df['Name'].value_counts()))
yvals = list(df['Name'].value_counts())
for i in range(len(df['Name'].value_counts())):
    plt.text(x=xvals[i], y=yvals[i],s=df['Name'].value_counts(sort=False)[i])

plt.hist(df['Name'])
plt.show()

所以,我得到这样的结果; result2

我认为应该没那么困难,但是我找不到任何解决方案。

2 个答案:

答案 0 :(得分:2)

您可以尝试以下操作:

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

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

答案 1 :(得分:1)

pyplot.hist返回垃圾箱的长度和位置。您可以通过保存pyplot.hist的退货来利用此功能:

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

data = [['B', 1], ['C', 2], ['A', 3],['A', 4], ['B', 5], ['B', 6],['A', 7], ['B', 8], ['C', 9],['D',10]] 
df = pd.DataFrame(data, columns = ['Name', 'Count']) 

xvals = np.arange(len(df['Name'].value_counts()))
yvals = list(df['Name'].value_counts())

counts, bins, _ = plt.hist(df['Name'])

for n, b in zip(counts, bins):
        plt.gca().text(b + 0.1, n, str(n))  # +0.1 to center text

plt.show()

这导致: enter image description here

如果要删除空条的0.0s,请将for循环更改为此:

for n, b in zip(counts, bins):
    if n > 0:
        plt.gca().text(b + 0.1, n, str(n))  # +0.1 to center text

结果: enter image description here