[Tex/LaTex] pgfplots Line Histogram

histogrampgfplotsstatistics

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.

I would like a line, please.

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

\addplot +[
hist={bins=5,
  data min=0,
  data max=10,
  handler/.style={sharp plot},
  intervals=false
},
x filter/.expression={x+1} % shift curve to center point
] table[y=A, col sep=comma] {data.csv};

The handler style for hist, 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 the x 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.

enter image description here

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.3}

\usepackage{filecontents}
\begin{filecontents*}{data.csv}
A
2
3
4
2
3
4
5
6
9
9
1
\end{filecontents*}

\begin{document}

\begin{tikzpicture}
  \centering
\begin{axis}[
/pgf/number format/.cd,
use comma,
1000 sep={},
title={Title},
xlabel={X Label},
ylabel={Y Label},
xticklabel style={rotate=90, anchor=near xticklabel},
ytick distance=2,
width=\textwidth, %10.5cm
height=6cm,
ymin=0
]

\addplot [
ybar interval,
fill=blue!10,
hist={bins=5,
  data min=0,
  data max=10,
}
] table[y=A, col sep=comma] {data.csv};

%%%
\addplot +[
hist={bins=5,
  data min=0,
  data max=10,
  handler/.style={sharp plot},
  intervals=false
},
x filter/.expression={x+1} % shift curve to center point
] table[y=A, col sep=comma] {data.csv};

\end{axis}
\end{tikzpicture}
\end{document}
Related Question