pgfplots Histogram with Curves












0















Variations on this question have been asked many times before, but I have not been able to find a working solution. The closest I have seen are discussions on other stackexchanges concerning how to produce this with R.



I am trying to produce the following plot, with red and blue lines (the position of the lines is merely an estimate of where I expect them to appear in a solution, which must calculate their position based on the information in the data.csv file):



Example



Part of my difficulty in asking this question is that I am not sure of the correct terminology to describe the red and the blue lines that I have drawn on the plot. I do not think they are gaussian curves as they are not symmetrical, but nevertheless I have included gaussian code in the below MWE in case my implementation of it is simply wrong (there is a very, very small gaussian plot on the bottom-left of the above chart that I have clearly been unsuccessful in implementing).



MWE



documentclass{standalone}
usepackage{pgfplots}
usepackage{filecontents}
begin{document}

begin{filecontents*}{data.csv}
COLUMNA,COLUMNB
38,22
85,18
104,82
56,20
202,57
64,15
115,22
8,20
120,14
81,24
100,28
39,11
81,29
25,18
122,51
93,10
45,19
103,11
33,24
60,24
50,47
61,24
46,14
45,15
84,72
62,20
50,13
84,38
52,19
108,5
182,34
145,19
117,12
34,59
43,19
42,26
170,18
31,27
86,18
183,24
36,15
,21
,16
,26
end{filecontents*}

pgfmathdeclarefunction{gauss}{2}{pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}%
}

begin{tikzpicture}
centering
begin{axis}[
ybar,
/pgf/number format/.cd,
use comma,
1000 sep={},
title={Title},
xlabel={Bins},
ylabel={Instances},
x label style={at={(axis description cs:0.5,-0.1)},anchor=north},
y label style={at={(axis description cs:0.05,0.5)},anchor=south},
%xticklabel style={rotate=90, anchor=near xticklabel},
xtick distance=50,
ytick distance=2,
width=textwidth, %10.5cm
height=6cm,
axis y line*=left,
axis x line*=bottom,
ymin=0,
xmin=0,
xticklabel interval boundaries,
]

%%%
addplot +[blue,
fill opacity=0.5,
hist={bins=22,
data min=0,
data max=220,
}
] table[y=COLUMNA, col sep=comma] {data.csv};
addlegendentry{Series A}

addplot +[red,
fill opacity=0.5,
hist={bins=22,
data min=0,
data max=220,
}
] table[y=COLUMNB, col sep=comma] {data.csv};
addlegendentry{Series A}

addplot [fill=red!50, draw=none, domain=0:220] {gauss(1.86,2.12)};

end{axis}
end{tikzpicture}
end{document}








share



























    0















    Variations on this question have been asked many times before, but I have not been able to find a working solution. The closest I have seen are discussions on other stackexchanges concerning how to produce this with R.



    I am trying to produce the following plot, with red and blue lines (the position of the lines is merely an estimate of where I expect them to appear in a solution, which must calculate their position based on the information in the data.csv file):



    Example



    Part of my difficulty in asking this question is that I am not sure of the correct terminology to describe the red and the blue lines that I have drawn on the plot. I do not think they are gaussian curves as they are not symmetrical, but nevertheless I have included gaussian code in the below MWE in case my implementation of it is simply wrong (there is a very, very small gaussian plot on the bottom-left of the above chart that I have clearly been unsuccessful in implementing).



    MWE



    documentclass{standalone}
    usepackage{pgfplots}
    usepackage{filecontents}
    begin{document}

    begin{filecontents*}{data.csv}
    COLUMNA,COLUMNB
    38,22
    85,18
    104,82
    56,20
    202,57
    64,15
    115,22
    8,20
    120,14
    81,24
    100,28
    39,11
    81,29
    25,18
    122,51
    93,10
    45,19
    103,11
    33,24
    60,24
    50,47
    61,24
    46,14
    45,15
    84,72
    62,20
    50,13
    84,38
    52,19
    108,5
    182,34
    145,19
    117,12
    34,59
    43,19
    42,26
    170,18
    31,27
    86,18
    183,24
    36,15
    ,21
    ,16
    ,26
    end{filecontents*}

    pgfmathdeclarefunction{gauss}{2}{pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}%
    }

    begin{tikzpicture}
    centering
    begin{axis}[
    ybar,
    /pgf/number format/.cd,
    use comma,
    1000 sep={},
    title={Title},
    xlabel={Bins},
    ylabel={Instances},
    x label style={at={(axis description cs:0.5,-0.1)},anchor=north},
    y label style={at={(axis description cs:0.05,0.5)},anchor=south},
    %xticklabel style={rotate=90, anchor=near xticklabel},
    xtick distance=50,
    ytick distance=2,
    width=textwidth, %10.5cm
    height=6cm,
    axis y line*=left,
    axis x line*=bottom,
    ymin=0,
    xmin=0,
    xticklabel interval boundaries,
    ]

    %%%
    addplot +[blue,
    fill opacity=0.5,
    hist={bins=22,
    data min=0,
    data max=220,
    }
    ] table[y=COLUMNA, col sep=comma] {data.csv};
    addlegendentry{Series A}

    addplot +[red,
    fill opacity=0.5,
    hist={bins=22,
    data min=0,
    data max=220,
    }
    ] table[y=COLUMNB, col sep=comma] {data.csv};
    addlegendentry{Series A}

    addplot [fill=red!50, draw=none, domain=0:220] {gauss(1.86,2.12)};

    end{axis}
    end{tikzpicture}
    end{document}








    share

























      0












      0








      0








      Variations on this question have been asked many times before, but I have not been able to find a working solution. The closest I have seen are discussions on other stackexchanges concerning how to produce this with R.



      I am trying to produce the following plot, with red and blue lines (the position of the lines is merely an estimate of where I expect them to appear in a solution, which must calculate their position based on the information in the data.csv file):



      Example



      Part of my difficulty in asking this question is that I am not sure of the correct terminology to describe the red and the blue lines that I have drawn on the plot. I do not think they are gaussian curves as they are not symmetrical, but nevertheless I have included gaussian code in the below MWE in case my implementation of it is simply wrong (there is a very, very small gaussian plot on the bottom-left of the above chart that I have clearly been unsuccessful in implementing).



      MWE



      documentclass{standalone}
      usepackage{pgfplots}
      usepackage{filecontents}
      begin{document}

      begin{filecontents*}{data.csv}
      COLUMNA,COLUMNB
      38,22
      85,18
      104,82
      56,20
      202,57
      64,15
      115,22
      8,20
      120,14
      81,24
      100,28
      39,11
      81,29
      25,18
      122,51
      93,10
      45,19
      103,11
      33,24
      60,24
      50,47
      61,24
      46,14
      45,15
      84,72
      62,20
      50,13
      84,38
      52,19
      108,5
      182,34
      145,19
      117,12
      34,59
      43,19
      42,26
      170,18
      31,27
      86,18
      183,24
      36,15
      ,21
      ,16
      ,26
      end{filecontents*}

      pgfmathdeclarefunction{gauss}{2}{pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}%
      }

      begin{tikzpicture}
      centering
      begin{axis}[
      ybar,
      /pgf/number format/.cd,
      use comma,
      1000 sep={},
      title={Title},
      xlabel={Bins},
      ylabel={Instances},
      x label style={at={(axis description cs:0.5,-0.1)},anchor=north},
      y label style={at={(axis description cs:0.05,0.5)},anchor=south},
      %xticklabel style={rotate=90, anchor=near xticklabel},
      xtick distance=50,
      ytick distance=2,
      width=textwidth, %10.5cm
      height=6cm,
      axis y line*=left,
      axis x line*=bottom,
      ymin=0,
      xmin=0,
      xticklabel interval boundaries,
      ]

      %%%
      addplot +[blue,
      fill opacity=0.5,
      hist={bins=22,
      data min=0,
      data max=220,
      }
      ] table[y=COLUMNA, col sep=comma] {data.csv};
      addlegendentry{Series A}

      addplot +[red,
      fill opacity=0.5,
      hist={bins=22,
      data min=0,
      data max=220,
      }
      ] table[y=COLUMNB, col sep=comma] {data.csv};
      addlegendentry{Series A}

      addplot [fill=red!50, draw=none, domain=0:220] {gauss(1.86,2.12)};

      end{axis}
      end{tikzpicture}
      end{document}








      share














      Variations on this question have been asked many times before, but I have not been able to find a working solution. The closest I have seen are discussions on other stackexchanges concerning how to produce this with R.



      I am trying to produce the following plot, with red and blue lines (the position of the lines is merely an estimate of where I expect them to appear in a solution, which must calculate their position based on the information in the data.csv file):



      Example



      Part of my difficulty in asking this question is that I am not sure of the correct terminology to describe the red and the blue lines that I have drawn on the plot. I do not think they are gaussian curves as they are not symmetrical, but nevertheless I have included gaussian code in the below MWE in case my implementation of it is simply wrong (there is a very, very small gaussian plot on the bottom-left of the above chart that I have clearly been unsuccessful in implementing).



      MWE



      documentclass{standalone}
      usepackage{pgfplots}
      usepackage{filecontents}
      begin{document}

      begin{filecontents*}{data.csv}
      COLUMNA,COLUMNB
      38,22
      85,18
      104,82
      56,20
      202,57
      64,15
      115,22
      8,20
      120,14
      81,24
      100,28
      39,11
      81,29
      25,18
      122,51
      93,10
      45,19
      103,11
      33,24
      60,24
      50,47
      61,24
      46,14
      45,15
      84,72
      62,20
      50,13
      84,38
      52,19
      108,5
      182,34
      145,19
      117,12
      34,59
      43,19
      42,26
      170,18
      31,27
      86,18
      183,24
      36,15
      ,21
      ,16
      ,26
      end{filecontents*}

      pgfmathdeclarefunction{gauss}{2}{pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}%
      }

      begin{tikzpicture}
      centering
      begin{axis}[
      ybar,
      /pgf/number format/.cd,
      use comma,
      1000 sep={},
      title={Title},
      xlabel={Bins},
      ylabel={Instances},
      x label style={at={(axis description cs:0.5,-0.1)},anchor=north},
      y label style={at={(axis description cs:0.05,0.5)},anchor=south},
      %xticklabel style={rotate=90, anchor=near xticklabel},
      xtick distance=50,
      ytick distance=2,
      width=textwidth, %10.5cm
      height=6cm,
      axis y line*=left,
      axis x line*=bottom,
      ymin=0,
      xmin=0,
      xticklabel interval boundaries,
      ]

      %%%
      addplot +[blue,
      fill opacity=0.5,
      hist={bins=22,
      data min=0,
      data max=220,
      }
      ] table[y=COLUMNA, col sep=comma] {data.csv};
      addlegendentry{Series A}

      addplot +[red,
      fill opacity=0.5,
      hist={bins=22,
      data min=0,
      data max=220,
      }
      ] table[y=COLUMNB, col sep=comma] {data.csv};
      addlegendentry{Series A}

      addplot [fill=red!50, draw=none, domain=0:220] {gauss(1.86,2.12)};

      end{axis}
      end{tikzpicture}
      end{document}






      pgfplots statistics histogram





      share












      share










      share



      share










      asked 6 mins ago









      CraigCraig

      17712




      17712






















          0






          active

          oldest

          votes











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "85"
          };
          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: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          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%2ftex.stackexchange.com%2fquestions%2f471616%2fpgfplots-histogram-with-curves%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to TeX - LaTeX Stack Exchange!


          • 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%2ftex.stackexchange.com%2fquestions%2f471616%2fpgfplots-histogram-with-curves%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







          Popular posts from this blog

          Accessing regular linux commands in Huawei's Dopra Linux

          Can't connect RFCOMM socket: Host is down

          Kernel panic - not syncing: Fatal Exception in Interrupt