2

Always in my long learning TikZ, I would build a "real" histogram from the following set of data :

xi ni

1000 5

1200 8

1500 24

2500 13

3000 2

The idea is to group the data into amplitude classes with 300 as the minimum value xmin=800 and xmax = 3200. Also, I would not want the y axis but only the grid. There should also be a legend to indicate the unit area.

    \usemodule[tikz]
    \usemodule[pgfplots]
    \usepgfplotslibrary[statistics]
    \pgfplotsset{width=12cm, compat=1.11}
    \usemodule[filecontents]
    \starttext
    \starttikzpicture
    \switchtobodyfont[10pt]
    \startaxis[
         width=10cm,
         height=13cm,
         ybar interval,
         xtick=data, 
         xticklabel interval boundaries, 
         x tick label style= {rotate=90,anchor=east,/pgf/number format/1000 sep={}},
         axis y line=left,
         axis x line=bottom,
         enlarge x limits,
         ymin=0,
         thick,grid=major,  
              ]
        \addplot[blue,fill=green]
         coordinates {(800,5) (1100,8) (1400,24) (1700,0) (2000,0) (2300,13)
                 (2600,0) (2900,2) (3200,0)};
         \stopaxis
         \stoptikzpicture

enter image description here

enter image description here

Fabrice
  • 3,636
  • 1
  • 21
  • 29

1 Answers1

3

I made for my self a histogram this way:

\documentclass[11pt,a4paper]{article} 
\usepackage{geometry}
\geometry{margin=1in} 

\usepackage{tikz}
\usepackage{tkz-euclide}
\usetikzlibrary{calc,intersections,through,backgrounds,snakes}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}
\usepgfplotslibrary{statistics}
\usepackage{bchart}
\begin{center}
\begin{tikzpicture}
\begin{axis}[
width=1*\textwidth,
height=6cm,
xmin=-1.5,xmax=13.5,
ymin=0, ymax=7,
title=Histogram lichaamslengte,
ybar interval,
xticklabel={[\pgfmathprintnumber\tick;\pgfmathprintnumber\nexttick [}],
\addplot+[hist={bins=4, data max=12,data min=0}]
table[row sep=\\,y index=0] {
data\\
1\\ 2\\ 1\\ 5\\ 4\\ 10\\ 4\\ 5\\ 7\\ 10\\ 9\\ 8\\ 9\\ 9\\ 11\\
};
\addplot[sharp plot,mark=square*,black]
coordinates
{(-1.5,0) (1.5,3) (4.5,4) (7.5,2) (10.5,6) (13.5,0)};
\end{axis}
\end{tikzpicture} 
\end{center}

enter image description here

  • 1
    @ArneThank you for the code I am inspired. As you can see, there is a problem with the labels. – Fabrice Jan 21 '15 at 22:22
  • 1
    @Fabrice I think you want to much info in a to short space. Does changing the width of your histogram help? – Arne Timperman Jan 22 '15 at 12:26
  • It's better by enlarging, but I do not understand why the amplitude is not equal to 300. – Fabrice Jan 22 '15 at 18:06
  • 1
    the space between 800 and 3200 is 2400... dividing 2400 by 7 is never 300. So change the upper and or lower x value. Hiding the y-axis can be done with "hide y axis," And for a legend you can write "\legend{your text}," – Arne Timperman Jan 23 '15 at 15:08
  • If I delete the y-axis, I have not the grid. In the legend, I have to display a top right square to indicate the unit area, for example 1cm² = 2. – Fabrice Jan 23 '15 at 23:42
  • But what if our data is already a frequency table? – Say OL Sep 17 '18 at 03:37
  • @SayOL is this https://tex.stackexchange.com/questions/153325/how-to-compute-an-histogram-plot-bars-binning-the-values-from-a-csv-using-tex helping you? – Arne Timperman Sep 17 '18 at 14:15
  • The data given are in individual. In some cases, the data given is by frequency and I do not know how to achieve it – Say OL Sep 18 '18 at 09:45