如何在直方图中获取每个条形的中点,并使用matplotlib画一条连接这些中点的线。
以下是样本数据
test_scores = [55,45,88,75,43,56,89,98,55,54,65,77,88,81,82,89,92,98,65,\
76,76,73,72,84]
test_scores1 = [55,45,88,73,43,55,89,98,44,54,65,77,80,81,84,89,92,98,65,\
71,75,73,70,81]
bins = [10,20,30,40,50,60,70,80,90,100]
plt.hist(test_scores, bins, histtype='bar')
plt.hist(test_scores1, bins, histtype='bar')
plt.show()
答案 0 :(得分:0)
hist()
函数返回分档的边缘及其高度。我们可以用它来计算条形的中心并将它们连接成一条线,如下所示:
import numpy as np
import matplotlib.pyplot as plt
test_scores = [55,45,88,75,43,56,89,98,55,54,65,77,88,81,82,89,92,98,65,\
76,76,73,72,84]
test_scores1 = [55,45,88,73,43,55,89,98,44,54,65,77,80,81,84,89,92,98,65,\
71,75,73,70,81]
bins = [0,10,20,30,40,50,60,70,80,90,100]
#y,edges,_ = plt.hist(test_scores1, bins)
y,edges = np.histogram(test_scores1, bins)
centers = 0.5*(edges[1:]+ edges[:-1])
plt.plot(centers,y,'-*')
plt.show()