Matplotlib的hist
说“计算并绘制x的直方图”。我想制作一个情节,不用先计算任何东西。我有bin宽度(不相等),以及每个bin中的总量,我想绘制一个频率 - 数量直方图。
例如,使用数据
cm Frequency
65-75 2
75-80 7
80-90 21
90-105 15
105-110 12
它应该是这样的情节:
http://www.gcsemathstutor.com/histograms.php
其中块的面积代表每个类中的频率。
答案 0 :(得分:2)
您需要bar chart:
import numpy as np
import matplotlib.pyplot as plt
x = np.sort(np.random.rand(6))
y = np.random.rand(5)
plt.bar(x[:-1], y, width=x[1:] - x[:-1])
plt.show()
此处x
包含条形的边缘,y
包含高度(不是区域!)。请注意,x
中的元素多于y
中的元素,因为还有一个边缘而不是条形。
使用原始数据和面积计算:
from __future__ import division
import numpy as np
import matplotlib.pyplot as plt
frequencies = np.array([2, 7, 21, 15, 12])
bins = np.array([65, 75, 80, 90, 105, 110])
widths = bins[1:] - bins[:-1]
heights = frequencies/widths
plt.bar(bins[:-1], heights, width=widths)
plt.show()
答案 1 :(得分:1)
像David Zwicker一样工作:
import numpy as np
import matplotlib.pyplot as plt
freqs = np.array([2, 7, 21, 15, 12])
bins = np.array([65, 75, 80, 90, 105, 110])
widths = bins[1:] - bins[:-1]
heights = freqs.astype(np.float)/widths
plt.fill_between(bins.repeat(2)[1:-1], heights.repeat(2), facecolor='steelblue')
plt.show()