条形图中某些移位条形的描述节点

条形图中某些移位条形的描述节点

在下面的例子中,我想要为中间分组中的每个条形图添加一个标签节点。如何才能让标签像条形图一样移动到正确的位置?

\documentclass[margin=5pt]{standalone}

\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
    title=First quarter comparison,
    width=5in,
    height=3in,
    ybar=0pt,
    xtick={2000, 2010, 2020},
    xtick style={draw=none},
    x tick label style={/pgf/number format/1000 sep=},
    ymin=0,
]

\pgfplotstableread{
   year   jan feb mar
   2000   3   2   3
   2010   4   5   5
   2020   2   2   1
}\mytable
\def\barcolor{blue}

\foreach \month [count=\n] in {Jan,Feb,Mar} {
    \edef\temp {
        \noexpand\addplot+ [draw=\barcolor, fill=\barcolor!20]
            table [x=year, y index=\n] {\noexpand\mytable};
        \noexpand\node [
            font=\noexpand\footnotesize,
            anchor=west,
            rotate=90,
        ] at (2010, 0) {\month};
    }
    \temp
}

\end{axis}
\end{tikzpicture}

\end{document}

在此处输入图片描述

答案1

我不是pgfplot专家。

编辑2

我们bar width=1可以使用 2009、2010 和 2011 来放置一月、二月和三月

\documentclass[margin=5pt]{standalone}
%https://tex.stackexchange.com/questions/675078/descriptive-nodes-for-certain-shifted-bars-in-bar-plot
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

\begin{document}

\begin{tikzpicture}

\tikzset{month/.style={font=\footnotesize,anchor=west,rotate=90}}
\begin{axis}[
    title=First quarter comparison,
    width=5in,
    height=3in,
    ybar=0pt,
    bar width=1,%<-- ajout
    xtick={2000, 2010, 2020},
    xtick style={draw=none},
    x tick label style={/pgf/number format/1000 sep=},
    ymin=0,
]

\pgfplotstableread{
year   jan feb mar
2000   3   2   3
2010   4   5   5
2020   2   2   1
}\mytable

\foreach \i in {1,...,3} {
    \addplot+ table [y index=\i] \mytable;
    }

    \node [month] at (2009,0) {jan};
    \node [month] at (2010,0) {fev};
    \node [month] at (2011,0) {mar};
\end{axis}
\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容