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} 
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.

6
  • 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?
    – Sigur
    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.
    – Sigur
    Apr 7 '17 at 1:36
  • 1
    @Sigur You have seen the answer of Roald? Apr 7 '17 at 1:44
  • Perfect. Exactly what I need. Thanks so much.
    – Sigur
    Apr 7 '17 at 1:56
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}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.