0

I have an interval 0.0..1.0 and heights of 10 bins inside it, for example:

[0.1, 0.2, 0.3, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1]

How can I render a histogram of these bins with the same width using Matplotlib?

Commander Tvis
  • 2,244
  • 2
  • 15
  • 41
  • 1
    In your example, are all bins supposed to have the _same_ height? If so, it is not a good example. – DYZ Mar 15 '20 at 06:29
  • @DYZ, the key problem is that Matplotlib's `pyplot.hist` function groups input values itself, but my input data is already grouped and the heights of the bins are known. – Commander Tvis Mar 17 '20 at 13:10

2 Answers2

1

enter image description here

It's clearly explained in the doc string of 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()
gboffi
  • 22,939
  • 8
  • 54
  • 85
0

Alternatively to the hist function, you could make use of the bar function.

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)
Monkey Pylot
  • 123
  • 1
  • 9