Search code examples
pythonmatplotlibmathhistogrambins

How to draw a histogram of bins of the same width and different height in a certain interval?


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?


Solution

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