如何绘制宽度不等的直方图而不用原始数据计算?(How to plot a histogram with unequal widths without computing it from raw dat

编程入门 行业动态 更新时间:2024-10-24 17:27:38
如何绘制宽度不等的直方图而不用原始数据计算?(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 12

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

直方图使用fill_between

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

histogram using fill_between

更多推荐

本文发布于:2023-07-20 02:56:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1192088.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:直方图   宽度   原始数据   plot   histogram

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!