0

I am preparing a material for students about Statistics and I have this generic-foo frequency polygon as an example, without the corresponding Histogram. I would like all intervals to be displayed in the x axis. If I write intervals=true key in hist, it shows all intervals but with a dot outside the plot quadrant, and when I change the key from true to false, it shows only some intervals but not the last one. Also the plotted dots are not centered with respect to the intervals or the vertical lines. How can I fix this?

enter image description here

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
    \usetikzlibrary{calc,intersections}
\usepackage{pgfplots}
    \usepgfplotslibrary{statistics}
    \pgfplotsset{compat=1.15}

\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        ymin=0, ymax=8,
        xmin=0, xmax=15,
        title={\color{blue} Polígono de Frecuencias},
        ybar interval,
        xticklabel={[\pgfmathprintnumber\tick--\pgfmathprintnumber\nexttick[}]
        ]
    \addplot[color=blue,forget plot,mark=*, hist={bins=5,data min=0,data max=15,handler/.style={sharp plot},intervals=false},shift={(axis direction cs:2,0) } ]
        table[row sep=\\,y index=0] 
        {data\\ 
        5\\ 14\\ 7\\ 1\\ 4\\ 9\\ 4\\ 5\\ 1\\ 10\\ 9\\ 8\\ 10\\ 9\\ 11\\ 2\\ 
        };
    \end{axis}
\end{tikzpicture}

\end{document}
  • Have a look here first: en.wikipedia.org/wiki/Histogram – hpekristiansen Sep 13 '17 at 22:40
  • 1
    @hpekristiansen Nice article at Wikipedia, but I could not find any reference of LaTeX in there or any possible solution to my issue. If your suggestion was in the direction of the missing histogram on my plot, I can deal with math accuracy later ;) . Thank you! – Cragfelt Sep 13 '17 at 23:05
  • 1
    Sorry for just the reference without explanation. I never meant for the article to help you with LaTeX, but to show you how a real histogram is made. You need to follow the guidelines in book/other teaching material, but be aware that histograms are thought in many many different ways and often plain wrong. – hpekristiansen Sep 13 '17 at 23:49
2

Here is a suggestion using an "empty" plot to get the desired tick description:

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}% loads also tikz
\pgfplotsset{compat=1.15}
\usepgfplotslibrary{statistics}
\usetikzlibrary{calc,intersections}

\begin{document}
\begin{tikzpicture}
  \begin{axis}[
      ymin=0, ymax=8,
      xmin=0, xmax=15,
      title={\color{blue} Polígono de Frecuencias},
      ybar interval,
      xticklabel={[\pgfmathprintnumber\tick--\pgfmathprintnumber\nexttick[}],
    ]
    \pgfplotsset{hist={
      bins=5,
      data min=0,data max=15,
      handler/.style={sharp plot}
    }}
    % "empty" plot without shift and without intervals=false:
    \addplot[draw=none,forget plot,mark=none]table[row sep=\\,y index=0]{data\\0\\};

    \addplot[color=blue,forget plot,mark=*,
      hist={intervals=false},
      shift={(axis direction cs:1.5,0)}% <- changed
    ]table[row sep=\\,y index=0]{data\\ 
        5\\ 14\\ 7\\ 1\\ 4\\ 9\\ 4\\ 5\\ 1\\ 10\\ 9\\ 8\\ 10\\ 9\\ 11\\ 2\\ 
    };
  \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

  • Thank you, this is what I was looking for. Interesting trick of the "empty" plot. I did not know that pgfplot charges TikZas well. Nice! – Cragfelt Sep 19 '17 at 19:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.