I would like to add the annotations and axis arrows like this to my histogram

Here is my code so far:

\documentclass[border=3mm,
tikz,
preview
]{standalone}
\usepackage{pgfplots}

\begin{document}
%---------------------------------------------------------------%
\begin{tikzpicture}
\begin{axis}[
ymin=0, ymax=6,
xmin = -0.5, xmax = 8,
area style,
]
\addplot+[ybar interval] plot coordinates { (-0.50, 1) (0.5, 4) (1.5, 5) (2.5, 3) (3.5, 2) (4.5, 1) (5.5, 0) };
\end{axis}
\end{tikzpicture}
\end{document}


Most questions are answered by the pgfplots manual:

\documentclass[border=3mm,
tikz,
preview
]{standalone}
\usepackage{pgfplots}

\begin{document}
%---------------------------------------------------------------%
\begin{tikzpicture}
\begin{axis}[
ymin=0, ymax=6,
xmin=-0.5, xmax=8,
xtick={0, ..., 7},
ytick={0, ..., 5},
axis x line=bottom,
axis y line=left,
area style,
]
(-0.50, 1) (0.5, 4) (1.5, 5) (2.5, 3) (3.5, 2) (4.5, 1) (5.5, 0)
};
\path
\foreach[count=\i from 0] \v in {1, 4, 5, 3, 2, 1, 0} {
(\i, \v) node[below] {\v}
}
(axis description cs:1, 0) node[above left, align=center] {g(?)\\level}
;
\end{axis}
\end{tikzpicture}
\end{document}


Remarks:

• The axis lines were unclear to me. Using both boxed style and arrows is too much for my taste, thus the examples shows the arrows style only.

• There are too many tick values. Since the bars are already annotated with the y values, the y tick values are superfluous, thus sparse numbers as in the default setting are better.

• The x tick values 6 and 7 do not have much of a purpose without data points/bars.

• Is it possible to write the number over the bars in automatic way? I mean, is PGF able to compute those numbers to print them? Apr 7 '17 at 0:05
• @Sigur Change \node[below] to \node[above] to get the numbers above the bars. Apr 7 '17 at 1:00
• Sorry, you misunderstood maybe. I would like to ask PGF to compute the values, not passing in \v in {} procedure. Apr 7 '17 at 1:36
• @Sigur You have seen the answer of Roald? Apr 7 '17 at 1:44
• Perfect. Exactly what I need. Thanks so much. Apr 7 '17 at 1:56

An alternative and perhaps easier approach, since you do not have to change your \foreach statement if you add or remove data.

• Using nodes near coords and the options every node near coord/.append style={anchor=north}, you can place nodes at or next to the coordinates of your data.
• In order for this to work nicely I changed the coordinates slightly and set the width of the bars to 1, using bar width=1.
• Finally I placed the xlabel at the right position using xlabel style={at=(current axis.south east), anchor=south east, text width=1 cm}

## Code

\documentclass[border=3mm,
tikz,
preview
]{standalone}
\usepackage{pgfplots}

\begin{document}
%---------------------------------------------------------------%
\begin{tikzpicture}
\begin{axis}[
ymin=0, ymax=6,
xmin=-0.5, xmax=8,
xtick={0, ..., 7},
ytick={0, ..., 5},
axis x line=bottom,
axis y line=left,
area style,
every node near coord/.append style={
anchor=north,
},
nodes near coords,
xlabel = gray level,
xlabel style={at=(current axis.south east), anchor=south east, text width=1 cm},
bar width = 1,
]