title: python: rayleigh fit (histogram)
body: <p>I’m still approaching programming in python.
  For the first time i'm trying working with histograms and fit!</p>
  <p>In particular, i have a dataset and i made a histogram of it. At this point i should do a rayleigh fit but i can't figure out the correct way to set the parameters correctly.  I read that loc and scale, which should be the parameters of fit are usually set as 0 and 1. Obviously, in this way, the fit doesn’t work well!!! Is there someone who can help me?
  To be clear I'am attaching the code I am using.</p>
  <p>Thank you.</p>
  <pre><code>import os 
  import numpy as np
  import nrrd
  import nibabel as nib
  import matplotlib.pyplot as plt
  import matplotlib.image as mpimg
  import SimpleITK as sitk
  import scipy.stats
  from scipy.stats import rayleigh
  import math
  
  
  #fit
  
  # Sample from this Random variable
  x0 = np.array(fondi)
  
  # Adjust Distribution parameters
  loc, scale = stats.rayleigh.fit(x0) # (9.990726961181025, 
  4.9743913760956335)
  
  # Tabulate over sample range (PDF display):
  xl = np.linspace(x0.min(), x0.max())
  
  # Display Results:
  fig, axe = plt.subplots()
  
  axe.hist(x0,density=1, label=&quot;background&quot;)
  
  axe.plot(xl,stats.rayleigh(scale=scale, loc=loc).pdf(xl), label=&quot;Rayleigh&quot;)
  axe.set_title(&quot;Distribution Fit&quot;)
  axe.set_xlabel(&quot;Intensità&quot;)
  
  axe.legend()
  axe.grid()
  </code></pre>
  <p>my data (fondi) are this:[13 15 13 14 12 13 12 14 15 12 11 10 11 15 18 11 11 11 13 15 15 15 11 12
  13 12 15 15 15 12 12 11 14 16 11 13 14 16 17 24 21 16 20 18 18 19 21 22
  19 15 16 15 13 14 16 18 21 19 22 14 13 14 15 14 17 19 17 16 18 12 15 17
  17 16 17 16 19 17 14 13 16 16 13 15 17 17 20 18 17 12 19 14 15 15 14 13
  17 16 14 12 11 12 20 19 16 24 19 20 19 17 16 17 16 19 22 17 16 20 22 21
  22 20 14 18 16 19 20 17 20 22 20 22 19 17 13 16 18 14 16 20 20 18 19 19
  16 19 12 12 14 14 13 15 16 16 19 16 17 12 11 11 10 12 11 11 13 14 13 17
  8  8  8 10 10 10 14 16 11  9  9 11 10 17 13 15 19 15 13 16 17 14 12 13
  14 11 10 15 13 12 12 11 10  9  9  9  9  8 15 16 12  9 11  9 10 10  7  7
  7 21 19 13 10 15 12 10 10  9  8 10 20 14 13 11 13 15 14 10 11 12 16 17
  15 12 13 16 15 13 14 17 14 13 15 13 11 14 15 17 18 22 21 16 17 22 17 17
  18 26 17 19 21 16 15 19 19 22 19 18 17 18 18 12 17 17 17 18 14 16 20 17
  16 16 18 16 19 18 18 20 18]</p>
  <p>OUTPUT: loc=6.783540954380711 scale=6.430045149216335</p>
  <p><a href="https://i.stack.imgur.com/Szn8d.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Szn8d.png" alt="Rayleight fit" /></a></p>