分组条形图,每条条形上方都有水平线

分组条形图,每条条形上方都有水平线

我想做的事:
在具有符号 x 轴坐标的分组条形图的每个条形上方放置水平线。这些线应与每个条形的宽度对齐。请参见代码中的 2 个带线示例条形和下图。
请注意,这些线用于指示每个条形的“上限”。因此,每条线都有自己的 y 坐标,该坐标与条形的实际高度无关。
我目前拥有的:
我只想出了一个不能令人满意的解决方案:
通过 \draw 调用绘制简单的线条并手动近似 x 坐标。
为什么这是个问题?
这会导致 x 坐标计算中出现非常丑陋的“魔法”数字(例如 !.655!、!.862!),如下面的代码所示。由于我有很多条形图,并且每组的条形图数量可能会发生变化,因此这变得非常繁琐。
解决我的问题还需要什么?
我正在寻找一种更方便的方法来计算(或获取)各个条形的最小/最大 x 坐标。利用下图中用黑色勾号表示的这些坐标,我可以轻松绘制所需的线条。

我目前的方法基于以下内容平均能量损失

\documentclass[11pt]{standalone}

\usepackage{tikz}
\usetikzlibrary{calc}

\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
    ybar,
    enlargelimits=0.15,
    ylabel={index},
    symbolic x coords={1982, 1990, 1999, 2006},
    legend style={at={(0.5,-0.15)},
    anchor=north,legend columns=-1},
    ]

\addplot coordinates {(1982, 1.78) (1990, 1.71) (1999, 1.68) (2006, 1.62)};
\addplot coordinates {(1982,  1.70) (1990, 1.62) (1999, 1.59) (2006, 1.64)};
\addplot coordinates {(1982, 2.04) (1990, 1.96) (1999, 1.95) (2006, 1.91)};

\draw[blue, thick] ($(axis cs:1982, 1.72)!.655!(axis cs:1990, 1.72)$) -- ($(axis cs:1982, 1.72)!.862!(axis cs:1990, 1.72)$);
\draw[red , thick] ($(axis cs:1982, 1.68)!.89!(axis cs:1990, 1.68)$) -- ($(axis cs:1990, 1.68)!.115!(axis cs:1999, 1.68)$);

\legend{USA, Netherlands, {(West-)Germany}}

\end{axis}
\end{tikzpicture}

\end{document}

图像

非常感谢您的帮助。

编辑:澄清的问题。

答案1

所以你在搜索类似以下内容的内容?
有关详细信息,请查看代码的注释。

% used PGFPlots v1.15
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
    \pgfplotsset{compat=1.3}
    % move data to a table
    % add columns for error bars and state "NaN" where no bar should be shown
    \pgfplotstableread{
        x       y1      y2      y3      y1err   y2err
        1982    1.78    1.70    2.04    NaN     NaN
        1990    1.71    1.62    1.96    0.05    0.02
        1999    1.68    1.59    1.95    NaN     NaN
        2006    1.62    1.64    1.91    NaN     NaN
    }{\loadedtable}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        ybar,
        ylabel={index},
        % so you don't have to repeat the "x" data, replace the commented line
        % by the next two
        % (this only works if the first `\addplot' command has data points on
        %  all x coordinates)
%        symbolic x coords={1982, 1990, 1999, 2006},
        xtick=data,
        xticklabels from table={\loadedtable}{x},
        enlargelimits=0.15,
        legend style={
            at={(0.5,-0.15)},
            anchor=north,
            legend columns=-1,
        },
        % ---
        % moved common options here
        % ---
        % this is also needed for the replacement of `symbolic x coords'
        table/x expr=\coordindex,
        % now we add the horizontal lines
        error bars/y dir=plus,
        error bars/y explicit,
        % (inspired by <https://tex.stackexchange.com/a/19962/95441>)
        error bars/error bar style={
            % don't show the vertical line
            opacity=0,
        },
        % and here the modifications for the horizontal line
        error bars/error mark options={
            rotate=90,
            mark size=0.5*\pgfplotbarwidth,
            line width=0.4pt,   % this is the default line with
            line cap=rect,
            opacity=1,          % to revert the option from `error bar style'
        },
    ]

        \addplot table [y=y1,y error=y1err]   {\loadedtable};
        \addplot table [y=y2,y error=y2err]   {\loadedtable};
        \addplot table [y=y3]                 {\loadedtable};

        \legend{
            USA,
            Netherlands,
            {(West-)Germany},
        }
    \end{axis}
\end{tikzpicture}
\end{document}

该图显示了上述代码的结果

答案2

好的,我看了你问题的旧版本,我建议

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
    ybar,
    enlargelimits=0.15,
    ylabel={index},
    symbolic x coords={1982, 1990, 1999, 2006},
    %xtick=data,
    %tick label style={font=\footnotesize},
    legend style={at={(0.5,-0.15)},
    anchor=north,legend columns=-1},
    nodes near coords,
    every node near coord/.append style={font=\tiny},
    nodes near coords={\underline{\pgfmathprintnumber{\pgfplotspointmeta}}},
   nodes near coords align={vertical},
    ]
\addplot coordinates {(1982, 1.78) (1990, 1.71) (1999, 1.68) (2006, 1.62)};
\addplot coordinates {(1982,  1.70) (1990, 1.62) (1999, 1.59) (2006, 1.64)};
\addplot coordinates {(1982, 2.04) (1990, 1.96) (1999, 1.95) (2006, 1.91)};
\legend{USA, Netherlands, {(West-)Germany}}

\end{axis}
\end{tikzpicture}

\end{document}

在此处输入图片描述

编辑:好的,现在来谈谈你的问题的第二个版本(?):

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
    ybar,
    bar width=12pt,
    enlargelimits=0.15,
    ylabel={index},
    symbolic x coords={1982, 1990, 1999, 2006},
    %xtick=data,
    %tick label style={font=\footnotesize},
    legend style={at={(0.5,-0.15)},
    anchor=north,legend columns=-1},
    nodes near coords,
    every node near coord/.append style={font=\tiny},
    nodes near coords={\rule{12pt}{1pt}},
   nodes near coords align={vertical},
    ]
\addplot coordinates {(1982, 1.78) (1990, 1.71) (1999, 1.68) (2006, 1.62)};
\addplot coordinates {(1982,  1.70) (1990, 1.62) (1999, 1.59) (2006, 1.64)};
\addplot coordinates {(1982, 2.04) (1990, 1.96) (1999, 1.95) (2006, 1.91)};
\legend{USA, Netherlands, {(West-)Germany}}

\end{axis}
\end{tikzpicture}

\end{document}

在此处输入图片描述

选择的长度\rule与 相一致bar width

相关内容