Why are my histogram bars coming out at the same height?

Question:

Any idea why my histogram turns out with the bars all at the same height ( yaxis 0.10)?

import pandas as pd
import numpy as np 
from matplotlib import pyplot as plt

np_lst = [33.48722283633526, 16.015345601144418, 7.9556538136419785, 7.146108329540282, 7.087586969243774, 6.9575395019182, 6.01144417712465, 5.939918070095585, 5.5530268548020025, 3.8461538461538463]

a = np.array([np_lst])

fig, ax = plt.subplots(figsize = (20,7))
ax.hist(a, bins = [0,10,20,30,40])

Answers:

I think, You don’t need the array:

fig, ax = plt.subplots(figsize = (20,7))
ax.hist(np_lst, bins = [0,10,20,30,40])

enter image description here

The square brackets at a = np.array([np_lst]) create a (1, 10) shaped numpy array:

a = np.array([np_lst])
print(a)
print(f'shape: {a.shape}')
[[33.48722284 16.0153456   7.95565381  7.14610833  7.08758697  6.9575395
   6.01144418  5.93991807  5.55302685  3.84615385]]
shape: (1, 10)

From which the bins aren’t derived as expected but you get the 10 bars with heigt = 1 on the respective x-axis values from on your np_lst.


The reason is that the square brackets for the np.array assignement are already in your np_lst:

print(np_lst)
[33.48722283633526, 16.015345601144418, 7.9556538136419785, 7.146108329540282,
 7.087586969243774, 6.9575395019182, 6.01144417712465, 5.939918070095585, 
5.5530268548020025, 3.8461538461538463]

So with np.array([np_lst]) it’s basically the same as:

np.array([[33.48722284 16.0153456 ...

Which resembles a n-D array setup.


Without the extra square brackets a = np.array(np_lst) you get:

a = np.array(np_lst)
print(a)
print(f'shape: {a.shape}')
[33.48722284 16.0153456   7.95565381  7.14610833  7.08758697  6.9575395
  6.01144418  5.93991807  5.55302685  3.84615385]
shape: (10,)

Which is assigned to the bins as expected:

(array([8., 1., 0., 1.]),
 array([ 0, 10, 20, 30, 40]),
 <BarContainer object of 4 artists>)
Answered By: MagnusO_O
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.