6

Matplotlibhist说“计算并绘制 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

其中块的面积代表每个类别中的频率。

4

2 回答 2

3

你想要一个条形图

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], align='edge')

plt.show()

这里x包含条的边缘并y包含高度(不是区域!)。请注意,inx比 in多一个元素,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, align='edge')

plt.show()

宽度不等的直方图

于 2013-07-02T15:51:45.200 回答
3

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

使用 fill_between 的直方图

于 2013-07-03T03:43:01.500 回答