如何绘制一定区间内宽度相同、高度不同的bin的直方图?
How to draw a histogram of bins of the same width and different height in a certain interval?
我有一个间隔 0.0
..1.0
和里面 10 个 bin 的高度,例如:
[0.1, 0.2, 0.3, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1]
如何使用 Matplotlib 渲染具有相同宽度的这些 bin 的直方图?
在hist
的文档字符串中有清楚的解释
Docstring:
Plot a histogram.
Compute and draw the histogram of *x*. The return value is a tuple
(*n*, *bins*, *patches*) or ([*n0*, *n1*, ...], *bins*, [*patches0*,
*patches1*, ...]) if the input contains multiple data. See the
documentation of the *weights* parameter to draw a histogram of
already-binned data.
In [24]: import numpy as np
...: import matplotlib.pyplot as plt
...:
...: w = [0.1, 0.2, 0.3, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1]
...: n = len(w)
...:
...: left, right = 0.0, 1.0
...: plt.hist(np.arange(left+d/2, right, d),
...: np.arange(left, right+d/2, d),
...: weights=w, rwidth=0.7)
...: plt.show()
除了 hist
函数,您还可以使用 bar
函数。
import matplotlib.pyplot as plt
xs = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]
heights = [0.1, 0.2, 0.3, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1]
bar_width = 0.08
plt.bar(xs, heights, bar_width)
我有一个间隔 0.0
..1.0
和里面 10 个 bin 的高度,例如:
[0.1, 0.2, 0.3, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1]
如何使用 Matplotlib 渲染具有相同宽度的这些 bin 的直方图?
在hist
Docstring: Plot a histogram. Compute and draw the histogram of *x*. The return value is a tuple (*n*, *bins*, *patches*) or ([*n0*, *n1*, ...], *bins*, [*patches0*, *patches1*, ...]) if the input contains multiple data. See the documentation of the *weights* parameter to draw a histogram of already-binned data.
In [24]: import numpy as np
...: import matplotlib.pyplot as plt
...:
...: w = [0.1, 0.2, 0.3, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1]
...: n = len(w)
...:
...: left, right = 0.0, 1.0
...: plt.hist(np.arange(left+d/2, right, d),
...: np.arange(left, right+d/2, d),
...: weights=w, rwidth=0.7)
...: plt.show()
除了 hist
函数,您还可以使用 bar
函数。
import matplotlib.pyplot as plt
xs = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]
heights = [0.1, 0.2, 0.3, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1]
bar_width = 0.08
plt.bar(xs, heights, bar_width)