5

我想用点和误差线绘制直方图。我不想要条形图或阶梯直方图。这可能吗?谷歌没有帮助我,我希望你能。它也不应该被标准化。谢谢!

4

2 回答 2

2

假设您使用的是 numpy 和 matplotlib,您可以使用 获取 bin 边缘和计数np.histogram(),然后用于pp.errorbar()绘制它们:

import numpy as np
from matplotlib import pyplot as pp

x = np.random.randn(10000)
counts,bin_edges = np.histogram(x,20)
bin_centres = (bin_edges[:-1] + bin_edges[1:])/2.
err = np.random.rand(bin_centres.size)*100
pp.errorbar(bin_centres, counts, yerr=err, fmt='o')

pp.show()

在此处输入图像描述

我不确定您所说的“标准化”是什么意思,但是例如,将计数除以值的总数以便直方图总和为 1 会很容易。

对我来说更大的问题是,在直方图的上下文中,误差线的实际含义是什么,您正在处理每个 bin 的绝对计数。

于 2013-10-07T11:05:49.897 回答
0

您也可以在纯 Matplotlib 中执行此操作,matplotlib.pyplot.hist用于绘制直方图。

import numpy as np               # Only used to generate the data
import matplotlib.pyplot as plt

x = np.random.randn(1000)

plt.figure()

# Matplotlib's pyplot.hist returns the bin counts, bin edges,
# and the actual rendered blocks of the histogram (which we don't need)
bin_counts, bin_edges, patches = plt.hist(x, bins=30)
bin_centres = (bin_edges[:-1] + bin_edges[1:]) / 2

# Generate some dummy error values, of the same dimensions as the bin counts
y_error = np.random.rand(bin_counts.size)*10

# Plot the error bars, centred on (bin_centre, bin_count), with length y_error
plt.errorbar(x=bin_centres, y=bin_counts,
             yerr=y_error, fmt='o', capsize=2)

plt.show()

带有误差线的直方图

于 2021-11-27T16:58:44.897 回答