0

This question is building on the solution given here. I wanted to add a thick line to the enteries that are "0" so that the histogram would look like this:

enter image description here

Here is the code:

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

\begin{document}
%---------------------------------------------------------------%
\begin{tikzpicture}
  \begin{axis}[
    ymin=0, ymax=10,
    xmin=-0.5, xmax=8,
    xtick={0, ..., 7},
    ytick={0, ..., 9},
    axis x line=bottom,
    axis y line=left,
    area style,
  ]
    \addplot+[ybar interval] plot coordinates {
      (-0.50, 0) (0.5, 9) (1.5, 7) (2.5, 0) (3.5, 2) (4.5, 6) (5.5, 0)
    };
    \path
      \foreach[count=\i from 0] \v in {0, 9, 5, 0, 2, 6, 0} {
        (\i, \v) node[below] {\v}
      }
      (axis description cs:1, 0) node[above left, align=center] {grey\\level}
    ;
  \end{axis}
\end{tikzpicture}
\end{document}
Joe
  • 9,080

2 Answers2

2

As Salim Bou said in a comment you can get the thin bar by setting the height to 0.05. I have also added one bar of height 0 starting at x=6.5, to get the bar at x=6 printed. Numbers printed below the x-axis is not shown so instead print them above.

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

\begin{document}
%---------------------------------------------------------------%
\begin{tikzpicture}
  \begin{axis}[
    ymin=0, ymax=10,
    xmin=-0.5, xmax=8,
    xtick={0, ..., 7},
    ytick={0, ..., 9},
    axis x line=bottom,
    axis y line=left,
    area style,
  ]
    \addplot+[ybar interval] plot coordinates {
      (-0.50, 0.05) (0.5, 9) (1.5, 7) (2.5, 0.05) (3.5, 2) (4.5, 6) (5.5, 0.05) (6.5, 0)};
    \path
      \foreach[count=\i from 0] \v in {0, 9, 5, 0, 2, 6, 0} {
        \ifnum\v>0
          (\i, \v) node[below] {\v}
        \else
          (\i, \v) node[above] {\v}
        \fi
      }
      (axis description cs:1, 0) node[above left, align=center] {grey\\level}
    ;
  \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

StefanH
  • 13,823
1

For example:

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

\begin{document}
%---------------------------------------------------------------%
\begin{tikzpicture}
  \begin{axis}[
    ymin=0, ymax=10,
    xmin=-0.5, xmax=9,
    xtick={0, ..., 7},
    ytick={0, ..., 9},
    axis x line=bottom,
    axis y line=left,
    area style,
  ]
    \addplot+[ybar interval] plot coordinates {
      (-0.50, 0) (0.5, 9) (1.5, 7) (2.5, 0) (3.5, 2) (4.5, 6) (5.5, 0)
    };
    \path
      \foreach[count=\i from 0] \v in {0, 9, 7, 0, 2, 6, 0} {
        (\i, \v) node[below] {\v}
      }
      (axis description cs:1, 0) node[above left, align=center] {grey\\level}
    ;
    \draw[ultra thick, blue]
      \foreach \x in {0, 3, 6, 7} {
        (\x -.5, 0) -- node[above=2pt, black] {0} (\x + .5, 0)
      }
    ;
  \end{axis}
\end{tikzpicture}
\end{document}

Result

Heiko Oberdiek
  • 271,626