2

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

enter image description here

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} 
Joe
  • 9,080

2 Answers2

5

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}

Result

enter image description here

Code

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

\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, 
  ]
    \addplot+[ybar] plot coordinates {
      (0, 1) (1, 4) (2, 5) (3, 3) (4, 2) (5, 1)
    };  
  \end{axis}
\end{tikzpicture}
\end{document}
Roald
  • 1,245
4

Most questions are answered by the pgfplots manual:

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

\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,
  ]
    \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)
    };
    \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} 

Result

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.

Heiko Oberdiek
  • 271,626