0

What's the meaning of count, bins, ignored in the code below which I found on the numpy website (https://numpy.org/doc/stable/reference/random/generated/numpy.random.lognormal.html).

import numpy as np
import matplotlib.pyplot as plt

mu, sigma = 0.2, 0.5 # mean and standard deviation

s = np.random.lognormal(mu, sigma, 1000)
count, bins, ignored = plt.hist(s, 100, density=True, align='mid')
x = np.linspace(min(bins), max(bins), 10000)

pdf = (np.exp(-(np.log(x) - mu)**2 / (2 * sigma**2))/ (x * sigma * np.sqrt(2 * np.pi)))
plt.plot(x, pdf, linewidth=2, color='r')
plt.axis('tight')
plt.show()
  • 2
    Look [here](https://matplotlib.org/3.5.1/api/_as_gen/matplotlib.pyplot.hist.html) (under _Returns_) – Timus Mar 15 '22 at 08:19

1 Answers1

0

count is the value for the density in each bin (in this example 100 values). bins contains the bin edges (101 in this example), where each pair of edges [i,i+1] are the edges of the bin. ignored is not important for the purpose of that plot. According to the documentation of plt.hist, it is a "Container of individual artists used to create the histogram or list of such containers if there are multiple input datasets".

PrinsEdje80
  • 494
  • 4
  • 8
  • 2
    Why would the bar container be "not important for the purpose of that plot"? Without them, you would have nothing to plot. Whether you want to catch them to modify them or not is a different topic. – Mr. T Mar 15 '22 at 08:25
  • 1
    Because the rest of the code does not use it. In the python convention, they could have also written `count, bins, _ = plt.hist...` where the `_` is a ["throwaway variable"](https://stackoverflow.com/questions/5893163/what-is-the-purpose-of-the-single-underscore-variable-in-python). Of course, if you really wanted, you could use it. Given the name "ignored" in this particular use of the code, it is actually ignored. For further/advanced used, please go ahead and use it. – PrinsEdje80 Mar 15 '22 at 08:43
  • How is bin defined in this case for 1000 values? –  Mar 15 '22 at 11:07
  • With 1001 values. See also the [plt.hist](https://matplotlib.org/3.5.1/api/_as_gen/matplotlib.pyplot.hist.html) documentation... – PrinsEdje80 Mar 15 '22 at 11:33