PGFPLOTS Histogram: Customizing X-Axis Labels

  • MHB
  • Thread starter Ackbach
  • Start date
  • Tags
    Histogram
In summary, the conversation discusses creating a histogram using the tikz and pgfplots packages. The main topic is how to label the x-axis boundaries instead of the intervals. The conversation explores different options and ultimately suggests using the "xtick=data" command and assigning bins on nice coordinates.
  • #1
Ackbach
Gold Member
MHB
4,155
93
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!
 
Physics news on Phys.org
  • #2
Ackbach said:
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?
 
  • #3
Sure. Here's a screenshot of a histogram produced by code very similar to what I posted.

View attachment 1947

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?
 

Attachments

  • Histogram.png
    Histogram.png
    2.5 KB · Views: 129
  • #4
Hmm, you mean something like this?

View attachment 1948

[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]
 

Attachments

  • tikz_histogram.png
    tikz_histogram.png
    2.7 KB · Views: 125
  • #5
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.
 
  • #6
Ackbach said:
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.
 
  • #7
Ackbach said:
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...

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]
 

Attachments

  • tikz_histogram2.png
    tikz_histogram2.png
    2.8 KB · Views: 110
  • #8
I like Serena said:
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?
 
  • #9
Ackbach said:
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.
 
  • #10
I like Serena said:
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.
 

FAQ: PGFPLOTS Histogram: Customizing X-Axis Labels

1. How do I change the number of ticks on the x-axis of a PGFPLOTS histogram?

To change the number of ticks on the x-axis, you can use the "xtick" option and specify the desired number of ticks. For example, "xtick={0,0.5,...,2}" would create ticks at 0, 0.5, 1, 1.5, and 2.

2. Can I use custom labels for the x-axis of my histogram?

Yes, you can use custom labels for the x-axis by using the "xticklabels" option and providing a list of labels in the same order as the ticks. For example, "xticklabels={low, medium, high}" would label the ticks as low, medium, and high.

3. How do I change the font size of the x-axis labels?

You can change the font size of the x-axis labels by using the "xticklabel style" option and specifying the desired font size. For example, "xticklabel style={font=\small}" would make the labels smaller.

4. Is it possible to rotate the x-axis labels?

Yes, you can rotate the x-axis labels by using the "xticklabel style" option and adding the "rotate" key with the desired rotation angle. For example, "xticklabel style={rotate=45}" would rotate the labels 45 degrees.

5. How do I add a title to the x-axis of my histogram?

To add a title to the x-axis, you can use the "xlabel" option and specify the desired title. For example, "xlabel={Time (seconds)}" would add the title "Time (seconds)" to the x-axis.

Similar threads

Replies
1
Views
5K
Back
Top