matplotlib - 如何在不根据原始数据计算的情况下绘制宽度不等的直方图?

标签 matplotlib histogram

Matplotlib 的 hist 说“计算并绘制 x 的直方图”。我想在不先计算任何东西的情况下制作一个情节。我有 bin 宽度(不相等)和每个 bin 中的总量,我想绘制频率-数量直方图。

例如,随着数据

cm      Frequency
65-75   2
75-80   7
80-90   21
90-105  15
105-110 12

它应该是这样的情节:

Histogram

http://www.gcsemathstutor.com/histograms.php

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

最佳答案

与 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

关于matplotlib - 如何在不根据原始数据计算的情况下绘制宽度不等的直方图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17429669/

相关文章:

python - Pandas 情节不重叠

python - 在不使用轮廓的情况下从 x、y、z 数据集绘制 3d 中的 matplotlib 颜色

python - 如何将彩色贴图图像反转为标量值?

python - 直方图绘制 "AttributeError: max must be larger than min in range parameter."

python - OpenCv RGB 直方图反投影未按预期工作

python - 缩小 Matplotlib

python - 更改文本 matplotlib 中的面部颜色

计算推土机距离的Java代码/库

python - Python 二维直方图

gnuplot - 绘制按额外列分组的直方图