I have a histogram. It is created from data in a CSV file.
I would like to connect the mid points on each bar with a smooth line.
My intention is to plot multiple series and show just the lines.
It is important that the bins remain centered under the mid-points.
MWE:
\begin{filecontents*}{data.csv}
A
2
3
4
2
3
4
5
6
9
9
1
\end{filecontents*}
\documentclass{standalone}
\usepackage{pgfplots}
\usepackage{filecontents}
\usepackage{pgfplotstable}
\begin{document}
\begin{tikzpicture}
\centering
\begin{axis}[
ybar interval,
/pgf/number format/.cd,
use comma,
1000 sep={},
title={Title},
xlabel={X Label},
ylabel={Y Label},
x label style={at={(axis description cs:0.5,-0.1)},anchor=north},
y label style={at={(axis description cs:0.05,0.5)},anchor=south},
xticklabel style={rotate=90, anchor=near xticklabel},
ytick distance=2,
width=\textwidth, %10.5cm
height=6cm,
ymin=0
]
%%%
\addplot +[
black,
fill=lightgray,
hist={bins=5,
data min=0,
data max=10,
}
] table[y=A, col sep=comma] {data.csv};
\end{axis}
\end{tikzpicture}
\end{document}
Best Answer
You can do this with
The
handler
style forhist
, described in the manual at the end of the section on histograms, is for just this sort of thing.intervals=false
is to avoid getting an extra data point at the end (see examples in manual), and finally thex filter
is to shift the curve to the mid points of the bars.Complete code, with the normal histogram included as well is below. Note I added
\pgfplotsset{compat=1.3}
. That setting will, at least, improve the default positioning of the axis labels, so your custom positioning might not be needed.