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

  • 2
    Pardon if I missed it, but what is your question? – darthbith Jan 20 '15 at 22:09
  • @ darthbith I modified my original question to clarify – Fabrice Jan 21 '15 at 22:19
  • Better no to change the original question, because the others can not see wat the problem was... – Arne Timperman Jan 22 '15 at 12:27
  • 1
    I still don't get what the question is. Can you make picture of how you want it to look like. – jens_bo Jan 25 '15 at 16:00
  • @ jens_bo The picture does not fit the data, but that's what I want to do. – Fabrice Jan 25 '15 at 20:05
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

|improve this answer|||||
  • 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

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.