Plotting two theoretical PDFs with each two histogram data set












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










share|improve this question





























    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










    share|improve this question



























      0












      0








      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










      share|improve this question
















      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







      python matplotlib






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 15 '18 at 16:01









      usr2564301

      17.7k73370




      17.7k73370










      asked Nov 15 '18 at 15:34









      Jonathan BudezJonathan Budez

      216




      216
























          1 Answer
          1






          active

          oldest

          votes


















          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)





          share|improve this answer


























          • 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 Dib
            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











          • Yes you can, see the edit in the answer.

            – Gerges Dib
            Nov 15 '18 at 15:57











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53322832%2fplotting-two-theoretical-pdfs-with-each-two-histogram-data-set%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          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)





          share|improve this answer


























          • 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 Dib
            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











          • Yes you can, see the edit in the answer.

            – Gerges Dib
            Nov 15 '18 at 15:57
















          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)





          share|improve this answer


























          • 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 Dib
            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











          • Yes you can, see the edit in the answer.

            – Gerges Dib
            Nov 15 '18 at 15:57














          1












          1








          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)





          share|improve this answer















          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)






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 15 '18 at 15:56

























          answered Nov 15 '18 at 15:46









          Gerges DibGerges Dib

          3,0131819




          3,0131819













          • 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 Dib
            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











          • Yes you can, see the edit in the answer.

            – Gerges Dib
            Nov 15 '18 at 15:57



















          • 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 Dib
            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











          • Yes you can, see the edit in the answer.

            – Gerges Dib
            Nov 15 '18 at 15:57

















          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





          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 Dib
          Nov 15 '18 at 15:54







          You have one point per bin. If you want more, you need to resample the bins.

          – Gerges Dib
          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





          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













          Yes you can, see the edit in the answer.

          – Gerges Dib
          Nov 15 '18 at 15:57





          Yes you can, see the edit in the answer.

          – Gerges Dib
          Nov 15 '18 at 15:57


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53322832%2fplotting-two-theoretical-pdfs-with-each-two-histogram-data-set%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Hercules Kyvelos

          Tangent Lines Diagram Along Smooth Curve

          Yusuf al-Mu'taman ibn Hud