Iaroslav Postovalov
Iaroslav Postovalov

Reputation: 2453

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?

Upvotes: 0

Views: 476

Answers (2)

Monkey Pylot
Monkey Pylot

Reputation: 142

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)

Upvotes: 0

gboffi
gboffi

Reputation: 25023

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

Upvotes: 1

Related Questions