如何绘制宽度不等的直方图而不用原始数据计算?(How to plot a histogram with unequal widths without computing it from raw data?)
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
块的面积代表每个类的频率。
Matplotlib's hist says "Compute and draw the histogram of x". I'd like to make a plot without computing anything first. I have the bin widths (unequal), and the total amount in each bin, and I want to plot a frequency-quantity histogram.
For instance, with the data
cm Frequency 65-75 2 75-80 7 80-90 21 90-105 15 105-110 12It should make a plot like this:
http://www.gcsemathstutor.com/histograms.php
where the area of the blocks represents the frequency in each class.
最满意答案
像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()Working on the same as 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()更多推荐
发布评论