PGFPLOTS:条形图条内的绘图

PGFPLOTS:条形图条内的绘图

我在绘制图表时遇到了以下问题xbar stacked。图表由三个数据集组成,我想在第二个数据集的开头和结尾处绘制两个箭头,以突出显示此栏中打印的值仅属于第二个数据集,而不是整体数据。

我创建了以下情节:

\documentclass[tikz]{standalone}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\usepackage{grffile}
\pgfplotsset{compat=newest}
\usetikzlibrary{plotmarks}
\usetikzlibrary{arrows.meta}

\usepackage{amsmath}

\begin{document}

\begin{tikzpicture}
        \begin{axis}
                [xbar stacked,
                width=8cm,
                height=2cm,
                symbolic y coords={naive},
                ytick=data,
                yticklabels={Naive},
                xlabel={Runtime $[s]$},
                legend columns=3,
                legend style={at={(0.5,1.05)}, anchor=south, legend cell align=left,
                align=left, legend plot pos=left, draw=black,font=\footnotesize},
                ]
                \draw[black, thick,<->] (axis cs: 100,naive) -- (axis cs: 1000,naive);
                \addplot+[green] coordinates {(100,naive)};
                \addlegendentry{Pre computations}
                %
                \addplot+[yellow,nodes near coords, node near coords style={black,font=\small\bfseries}]
                        coordinates {(900,naive)};
                \addlegendentry{Iteration}
                \addplot+[red] coordinates {(150,naive)};
                \addlegendentry{Post computations}
        \end{axis}
\end{tikzpicture}
\end{document}

得到以下图表: 绘图

通过插入

\draw[black, thick,<-] (axis cs: 100,naive) -- (axis cs: 250,naive);
\draw[black, thick,->] (axis cs: 850,naive) -- (axis cs: 1000,naive);

在执行第一个\addplot命令之前我实现了所需的输出: 通缉

但我的解决方案存在以下问题:

因此,我遇到了以下问题:

  • draw命令需要位于所有addplot命令之前,否则它将在后台运行。
  • 但更恼人的问题是:目前,我必须自己根据数据计算条形图的起点和终点。每个条形图的起点或终点是否有节点/坐标?在示例中:条形图黄色部分的起点和终点是否有自动生成的节点,或者可以自动设置此类节点?因为最后,我有大约 20 行,我不想手动计算两个箭头的位置。

答案1

我猜你正在搜索以下结果,对吗?

如果是,请先向前阅读注释\begin{axis},然后axis再向后阅读环境内部的注释。

您需要添加\draw命令命令\addplot,因为bar stacked图是用受人尊敬顺序。因此,举个例子,首先绘制红色条(从 0 到 1150),然后是黄色,然后是绿色。这样,黄色条就位于红色条之上,绿色条位于黄色条之上。当然,在第一个条之前说明其他内容\addplot可以确保这些内容也位于该条之上。

% used PGFPlots v1.16
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    \usetikzlibrary{arrows.meta}
    \pgfplotsset{
        compat=1.16,
        % define a style to use for the connecting lines
        /tikz/my line style/.style={
            black,
            thick,
            ->,
        },
    }
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        xbar stacked,
        width=8cm,
        height=2cm,
        symbolic y coords={naive},
        ytick=data,
        yticklabels={Naive},
        xlabel={Runtime [s]},
        legend columns=3,
        legend style={
            at={(0.5,1.05)},
            anchor=south,
            legend cell align=left,
            align=left,
            legend plot pos=left,
            draw=black,
            font=\footnotesize,
        },
        % !!! this is the critical point !!!
        % to make that solution work you have set the following option due to
        % technical reasons which is roughly:
        % normally the markers are first collected and are drawn *after*
        % `\end{axis}', in contrast to the `\draw' command. Said that, the
        % named `nodes near coords' are not available during the execution of
        % the `\draw' command
        clip marker paths=true,
    ]
        % draw the connecting lines using the predefined style
        \draw [my line style] (b-1-0) edge (a-0-1)
                                      edge (a-1-1)
        ;

        \addplot+[green] coordinates {(100,naive)}
            coordinate (a-\plotnum-\coordindex)
        ;
            \addlegendentry{Pre computations}

        \addplot+[
            yellow,
            nodes near coords,
            node near coords style={
                black,
                font=\small\bfseries,
                % also add names to the `nodes near coords`
                name=b-\plotnum-\coordindex,
            },
        ] coordinates {(900,naive)}
            % add coordinates to the points/bars
            coordinate (a-\plotnum-\coordindex)
        ;
            \addlegendentry{Iteration}

        \addplot+[red] coordinates {(150,naive)};
            \addlegendentry{Post computations}
    \end{axis}
\end{tikzpicture}
\end{document}

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

相关内容