0

I am trying to fit two histogram with two PDF curves using the following code (from Fitting a histogram with python):

datos_A = df['KWH/hh (per half hour) ']
datos_B = df['Response KWH/hh (per half hour) ']
(mu_A, sigma_A) = norm.fit(datos_A)
(mu_B, sigma_B) = norm.fit(datos_B)
n, bins, patches = plt.hist([datos_A , datos_B], 16, normed=1)
y_A = mlab.normpdf(bins, mu_A, sigma_A)
y_B = mlab.normpdf(bins, mu_B, sigma_B)
l = plt.plot([bins, bins], [y_A, y_B], 'r--', linewidth=2)
plt.grid(True)
plt.show()

However, I get something like this:

graph with vertical red lines

Instead of two PDF lines for each histogram, I get those vertical lines. I have tried to fix this in many ways but I still cannot figure it out.

After adjusting my code I am getting this two lines, however, they are not smooth curves.

graph with straight lines

Jongware
  • 22,200
  • 8
  • 54
  • 100
Jonathan Budez
  • 226
  • 1
  • 3
  • 12

1 Answers1

1

This is because plt.plot plots each curve row wise. Which means according to your example it plots n vertical lines since the x coordinates are (bins[i], bins[i]) for all lines.

To fix this change the line:

l = plt.plot([bins, bins], [y_A, y_B], 'r--', linewidth=2)

to:

l_A = plt.plot(bins, y_A, 'r--', linewidth=2)
l_B = plt.plot(bins, y_B, 'b--', linewidth=2)

OR:

l = plt.plot(bins, np.stack([y_A, y_B]).T, '--', lw=2)

EDIT:

To get smoother lines, you can resample the bins like so:

N_resample = 100
bins_resampled = np.linspace(min(bins), max(bins), N_resample)
y_A = mlab.normpdf(bins_resampled, mu_A, sigma_A)
y_B = mlab.normpdf(bins_resampled, mu_B, sigma_B)
l = plt.plot(bins_resampled, np.stack([y_A, y_B]).T, '--', lw=2)
Gerges
  • 6,269
  • 2
  • 22
  • 44
  • It solved the problem, however, I would like a smother line, because this looks more that different straight lines joined rather than one whole curve. I edited the picture to clarify the question – Jonathan Budez Nov 15 '18 at 15:53
  • You have one point per bin. If you want more, you need to resample the bins. – Gerges Nov 15 '18 at 15:54
  • I only want 16 bins for the histogram, but is it possible to define a different number of bins for the curve? – Jonathan Budez Nov 15 '18 at 15:56