Welcome to our community

Be a part of something great, join today!

TiKZ pgfplots Histogram Help

  • Thread starter
  • Admin
  • #1

Ackbach

Indicium Physicus
Staff member
Jan 26, 2012
4,192
Warning: requires the tikz and pgfplots packages.

I've got my histogram almost where I want it:

Code:
\begin{center}
\begin{tikzpicture}
\begin{axis}[
  tiny,
  width=6in,
  ymin=0,
  ybar interval,
  ]
  \addplot+[hist={bins=10,density}]
    table[row sep=\\,y index=0] {
    	data \\
      565 \\ 786 \\ 870 \\ 923 \\ 948 \\ 951 \\ 964 \\ 968 \\
      997 \\1007 \\1013 \\1037 \\1040 \\1051 \\1056 \\1080 \\
     1088 \\1090 \\1102 \\1103 \\1104 \\1120 \\1151 \\1159 \\
     1165 \\1185 \\1189 \\1207 \\1216 \\1233 \\1251 \\1256 \\
     1261 \\1292 \\1312 \\1317 \\1347 \\1358 \\1385 \\1416 \\
     1477 \\1500 \\1514 \\1567 \\1592 \\1588 \\1615 \\1713 \\
     2325 \\3168 \\};
\end{axis}
\end{tikzpicture}
\end{center}
The one thing I want to change is where the $x$-axis labels are showing up. Instead of labeling the intervals (which I think is extremely confusing and unhelpful unless you give the range, which won't fit here), I want to label the boundaries between the intervals. How can I get pgfplots to do that? Thanks!
 

dwsmith

Well-known member
Feb 1, 2012
1,673
Warning: requires the tikz and pgfplots packages.

I've got my histogram almost where I want it:

Code:
\begin{center}
\begin{tikzpicture}
\begin{axis}[
  tiny,
  width=6in,
  ymin=0,
  ybar interval,
  ]
  \addplot+[hist={bins=10,density}]
    table[row sep=\\,y index=0] {
        data \\
      565 \\ 786 \\ 870 \\ 923 \\ 948 \\ 951 \\ 964 \\ 968 \\
      997 \\1007 \\1013 \\1037 \\1040 \\1051 \\1056 \\1080 \\
     1088 \\1090 \\1102 \\1103 \\1104 \\1120 \\1151 \\1159 \\
     1165 \\1185 \\1189 \\1207 \\1216 \\1233 \\1251 \\1256 \\
     1261 \\1292 \\1312 \\1317 \\1347 \\1358 \\1385 \\1416 \\
     1477 \\1500 \\1514 \\1567 \\1592 \\1588 \\1615 \\1713 \\
     2325 \\3168 \\};
\end{axis}
\end{tikzpicture}
\end{center}
The one thing I want to change is where the $x$-axis labels are showing up. Instead of labeling the intervals (which I think is extremely confusing and unhelpful unless you give the range, which won't fit here), I want to label the boundaries between the intervals. How can I get pgfplots to do that? Thanks!

can you post a picture of what it looks like and what you want?
 
  • Thread starter
  • Admin
  • #3

Ackbach

Indicium Physicus
Staff member
Jan 26, 2012
4,192
Sure. Here's a screenshot of a histogram produced by code very similar to what I posted.

Histogram.png

Now you see the numbers on the horizontal axis - they're lined up with the intervals, and I find that rather ambiguous. I want to have numbers (they might be different from the ones currently showing) marking the boundaries of the intervals, not the intervals themselves, and I want the numbers aligned under the boundaries.

Does that further clarify what I'm after?
 

Klaas van Aarsen

MHB Seeker
Staff member
Mar 5, 2012
8,780
Hmm, you mean something like this?

tikz_histogram.png

[LATEXS]\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
width=6in,
ymin=0,
xtick=data,
]
\addplot[
ybar interval,
draw=black!60,
fill=blue!20,
mark=none,
] coordinates {
(750, 0.02) (1000, 0.16) (1250, 0.42) (1500, 0.24) (1750, 0.12) (2000, 0)
(2250, 0) (2500, 0.02) (2750, 0) (3000, 0) (3250, 0.02) (3500, 0.02)
};
\end{axis}
\end{tikzpicture}
\end{document}[/LATEXS]
 
  • Thread starter
  • Admin
  • #5

Ackbach

Indicium Physicus
Staff member
Jan 26, 2012
4,192
Yes! Very nice, thank you! I don't suppose you can get TiKZ to to that automatically? (I know, I'm only asking for the moon, here.) I see you have to compute the values yourself, which is fine. But if TiKZ could compute those for me, that would be AWESOME.
 

dwsmith

Well-known member
Feb 1, 2012
1,673
Yes! Very nice, thank you! I don't suppose you can get TiKZ to to that automatically? (I know, I'm only asking for the moon, here.) I see you have to compute the values yourself, which is fine. But if TiKZ could compute those for me, that would be AWESOME.
The pgfmathparser can do calculations. Check out the manual.
 

Klaas van Aarsen

MHB Seeker
Staff member
Mar 5, 2012
8,780
Yes! Very nice, thank you! I don't suppose you can get TiKZ to to that automatically? (I know, I'm only asking for the moon, here.) I see you have to compute the values yourself, which is fine. But if TiKZ could compute those for me, that would be AWESOME.
Mmmmh, suppose you remove the "ybar interval" so TikZ won't be confused which type of plot you want, and add "xtick=data" to indicate you want to use the x coordinates of the data as labels...

tikz_histogram2.png

[LATEXS]\documentclass{article}
\usepackage{tikz, pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[ tiny,
width=6in,
xtick=data,
ymin=0 ]
\addplot+[
hist={ bins=10, density },
fill=blue!20,
mark=none,
] table[ row sep=\\, y index=0 ] {
data \\ 565 \\ 786 \\ 870 \\ 923 \\ 948 \\ 951 \\ 964 \\ 968 \\
997 \\1007 \\1013 \\1037 \\1040 \\1051 \\1056 \\1080 \\
1088 \\1090 \\1102 \\1103 \\1104 \\1120 \\1151 \\1159 \\
1165 \\1185 \\1189 \\1207 \\1216 \\1233 \\1251 \\1256 \\
1261 \\1292 \\1312 \\1317 \\1347 \\1358 \\1385 \\1416 \\
1477 \\1500 \\1514 \\1567 \\1592 \\1588 \\1615 \\1713 \\
2325 \\3168 \\
};
\end{axis}
\end{tikzpicture}
\end{document}[/LATEXS]
 
  • Thread starter
  • Admin
  • #8

Ackbach

Indicium Physicus
Staff member
Jan 26, 2012
4,192
Mmmmh, suppose you remove the "ybar interval" so TikZ won't be confused which type of plot you want, and add "xtick=data" to indicate you want to use the x coordinates of the data as labels...

View attachment 1950

[LATEXS]\documentclass{article}
\usepackage{tikz, pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[ tiny,
width=6in,
xtick=data,
ymin=0 ]
\addplot+[
hist={ bins=10, density },
fill=blue!20,
mark=none,
] table[ row sep=\\, y index=0 ] {
data \\ 565 \\ 786 \\ 870 \\ 923 \\ 948 \\ 951 \\ 964 \\ 968 \\
997 \\1007 \\1013 \\1037 \\1040 \\1051 \\1056 \\1080 \\
1088 \\1090 \\1102 \\1103 \\1104 \\1120 \\1151 \\1159 \\
1165 \\1185 \\1189 \\1207 \\1216 \\1233 \\1251 \\1256 \\
1261 \\1292 \\1312 \\1317 \\1347 \\1358 \\1385 \\1416 \\
1477 \\1500 \\1514 \\1567 \\1592 \\1588 \\1615 \\1713 \\
2325 \\3168 \\
};
\end{axis}
\end{tikzpicture}
\end{document}[/LATEXS]
Better and better! The only catch with this is that according to my statistics book, it's not considered a good idea to have any data points fall directly on a boundary. Then it's confusing which bin that data point is supposed to be in. Now, if in the plot you gave, that only happens at the endpoints (possibly only one?), then I suppose that wouldn't be ambiguous. And if it does happen somewhere in the middle, maybe I could mitigate that by changing the number of bins. Does that sound reasonable?
 

Klaas van Aarsen

MHB Seeker
Staff member
Mar 5, 2012
8,780
Better and better! The only catch with this is that according to my statistics book, it's not considered a good idea to have any data points fall directly on a boundary. Then it's confusing which bin that data point is supposed to be in. Now, if in the plot you gave, that only happens at the endpoints (possibly only one?), then I suppose that wouldn't be ambiguous. And if it does happen somewhere in the middle, maybe I could mitigate that by changing the number of bins. Does that sound reasonable?
Pgfplots documentation says that each bin includes the left boundary and excludes the right boundary so there is no ambiguity. That is an interval of the type [a,b). As far as I'm concerned that is pretty standard.
That is with the exception of the rightmost bin that does include the right boundary, which is not ambiguous.
In this particular histogram both the leftmost bin and the rightmost bin have 1 data point on the boundary simply because the bins are bounded this way. That should be okay since there is no ambiguity.
Btw, my own preference is to assign bins on nice coordinates, as I did in the my first response.
 

Ackbach

Indicium Physicus
Staff member
Jan 26, 2012
4,192
Pgfplots documentation says that each bin includes the left boundary and excludes the right boundary so there is no ambiguity. That is an interval of the type [a,b). As far as I'm concerned that is pretty standard.
That is with the exception of the rightmost bin that does include the right boundary, which is not ambiguous.
In this particular histogram both the leftmost bin and the rightmost bin have 1 data point on the boundary simply because the bins are bounded this way. That should be okay since there is no ambiguity.
Btw, my own preference is to assign bins on nice coordinates, as I did in the my first response.
Gotcha. Thanks very much, I like Serena and dwsmith! You've been very helpful.