matplotlib.axes.Axes.hist : need to understand the relationship between input and output parameters

Asked

Viewed 28 times

0

For example, it has the following code:

import matplotlib 
import numpy as np 
import matplotlib.pyplot as plt 
  
np.random.seed(10**7) 
mu = 121  
sigma = 21
x = mu + sigma * np.random.randn(1000) 
  
num_bins = 100
fig, ax = plt.subplots() 
  
n, bins, patches = ax.hist(x, num_bins, 
                           density = 1,  
                           color ='green',  
                           alpha = 0.7) 
  
y = ((1 / (np.sqrt(2 * np.pi) * sigma)) *
     np.exp(-0.5 * (1 / sigma * (bins - mu))**2)) 
ax.plot(bins, y, '--', color ='black') 
ax.set_xlabel('X-Axis') 
ax.set_ylabel('Y-Axis') 
  
ax.set_title('matplotlib.axes.Axes.hist() Example') 
plt.show() 

With it is generated the following graph:

inserir a descrição da imagem aqui

My doubt: it is not clear to me how,

x = mu + sigma * np.random.randn(1000) 

which is then passed on

n, bins, patches = ax.hist(x,...

makes the histogram look like this. By the matplotlib documentation, the output parameter n "returns the values of the histogram boxes", but what operations are done in the x for the graph to be plotted this way?

I also don’t understand what the "Density = 1" parameter does on the graph.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.