显示超时(离开页面)的条形图

显示超时(离开页面)的条形图

我正在尝试制作一个条形图来显示某些程序的运行时间,并且我希望某些条形图超出页面顶部。目前,我通过将条形的值设置为较大的值来实现此目的(请参见下面的示例 - 对于程序 1,测试 3 最右侧的条形图的值为 10,但我希望它超出页面),但我实际上希望条形图缩放到条形图的高度并超出页面顶部。我还希望标签 TO(超时)位于右侧蓝色条的顶部,但目前它位于右侧蓝色和右侧条之间(请参见下面的示例)。有没有办法将 TO 标签放在最右侧的蓝色条上,而不是放在蓝色和红色条的中间?

这是我的代码:

\begin{figure*}
\begin{tikzpicture}
\begin{axis}[
    ylabel=Seconds,
    ymode=log,
    bar width=10pt,
    ybar,
    x tick label style={rotate=25},
    symbolic x coords={Test 1, Test 2, Test 3},
    xtick=data,
    legend pos = north west,
    height=7cm,
    width=\textwidth,
]
\addplot
    coordinates {
        (Test 1, 0.4)
        (Test 2, 1.5)
        (Test 3, 10)
};
\node[above] at (axis cs:Test 3, 10) {T.O.};
\addplot
    coordinates {
        (Test 1, 5.5)
        (Test 2, 4.6)
        (Test 3, 6)
};
\legend{Program 1, Program 2}
\end{axis}
\end{tikzpicture}
\caption{Program Runtimes}
\end{figure*}

以下是我的文件中的条形图:

在此处输入图片描述

我想要的条形图如下所示:

在此处输入图片描述

这可能吗?

答案1

您可以使用手动设置 y 轴的范围ymax,然后将超时栏设置为该值。这会导致栏延伸到绘图的边缘,而不会扩大绘图区域。

要打印绘图区域外的节点,您可以使用clip=false。要防止刻度标记与标签重叠,您可以使用 关闭顶部刻度标记xtick pos=left。您可以使用 将标签向左移动一点,xshift使其位于条形图上方的中心。

此外,我需要进行设置log origin=infty以确保条形图从图表的底部开始。

梅威瑟:

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{figure*}
\begin{tikzpicture}
\begin{axis}[
    ylabel=Seconds,
    ymode=log,
    log origin=infty,
    bar width=10pt,
    ybar,
    x tick label style={rotate=25},
    symbolic x coords={Test 1, Test 2, Test 3},
    xtick=data,
    legend pos = north west,
    height=7cm,
    width=\textwidth,
    ymax=15,
    clip=false,
    xtick pos=left,
]
\addplot
    coordinates {
        (Test 1, 0.4)
        (Test 2, 1.5)
        (Test 3, 15)
};
\node[above,xshift=-5pt] at (axis cs:Test 3, 15) {T.O.};
\addplot
    coordinates {
        (Test 1, 5.5)
        (Test 2, 4.6)
        (Test 3, 6)
};
\legend{Program 1, Program 2}
\end{axis}
\end{tikzpicture}
\end{figure*}
\end{document}

结果:

在此处输入图片描述

我为这个答案使用的参考资料:

pgfplots 和对数刻度:从屏幕底部开始的条形图

长节点不合要求地被裁剪到 pgfplot 之外

如何从用 PGFPlots 生成的图的顶部和右侧轴上删除刻度?

答案2

在这里我提出了我的答案的修改解决方案https://tex.stackexchange.com/a/365826/95441使其也适用于对数图。这样做的好处是,所有超出的条形图ymax都会“自动”获得文本。有关详细信息,请查看代码中的注释。

% used PGFPlots v1.18.1
% (modified version of <https://tex.stackexchange.com/a/365826/95441>)
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
        % create a variable to store the `ymax' value
        \pgfmathsetmacro{\ymax}{15}
    \begin{axis}[
        ymode=log,
        % (plot all bars from bottom (i.e. from the bottom axis line))
        log origin=infty,
        ybar,
        symbolic x coords={Test 1, Test 2, Test 3},
        xtick=data,
        % use the previously created variable here
        ymax=\ymax,
        % (this i just added so the outer most bars aren't clipped partially)
        enlarge x limits={0.2},
        % -----------------------------------------------------------------
        % we only want to print some text ...
        nodes near coords={T.O.},
        % ... when the y value is greater than `ymax`
        % (it is `ln` instead of `log10` because internally logarithmic values
        %  are calculated on the basis $e$)
        y filter/.code={\pgfmathparse{min(ln(y),ln(\ymax))}},
        % ---------------------------------------------------------------------
        % now we create a style for the `nodes near coords` which is dependent
        % on the value
        % (adapted from <http://tex.stackexchange.com/a/141006/95441>)
        % (#1: the THRESHOLD after which we switch to a special display)
        nodes near coords greater equal only/.style={
            % define the style of the nodes with "small" values
            small value/.style={
                /tikz/coordinate,
            },
            every node near coord/.append style={
                check for small values/.code={
                    \begingroup
                    % this group is merely to switch to FPU locally.
                    % Might be unnecessary, but who knows.
                    \pgfkeys{/pgf/fpu}
                    \pgfmathparse{\pgfplotspointmeta<(#1)}
                    \global\let\result=\pgfmathresult
                    \endgroup
                    %
                    % simplifies debugging:
                    %\show\result
                    %
                    \pgfmathfloatcreate{1}{1.0}{0}
                    \let\ONE=\pgfmathresult
                    \ifx\result\ONE
                        % AH: our condition 'y < #1' is met.
                        \pgfkeysalso{/pgfplots/small value}
                    \fi
                },
                check for small values,
            },
        },
        % assign a value to the new style which is the threshold at which
        % the `small value' style is used.
        % Of course in this case it should be the logarithmic `\ymax' value
        nodes near coords greater equal only=ln(\ymax),
        % -----------------------------------------------------------------
    ]
        \addplot coordinates {(Test 1, 0.4) (Test 2, 1.5) (Test 3, 16)};
        \addplot coordinates {(Test 1, 5.5) (Test 2, 4.6) (Test 3,  6)};
    \end{axis}
\end{tikzpicture}
\end{document}

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

相关内容