为直方图第 2 部分添加注释

为直方图第 2 部分添加注释

这个问题建立在这里给出的解决方案。我想在“0”项上添加一条粗线,以便直方图看起来像这样:

在此处输入图片描述

以下是代码:

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

答案1

例如:

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

结果

答案2

正如 Salim Bou 在评论中所说,您可以通过将高度设置为 来获得细条。0.05我还添加了一个高度为 的条,以打印条。打印在 x 轴下方的数字未显示,因此请将其打印在上方。0x=6.5x=6

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

在此处输入图片描述

相关内容