Tikz 条形图,坐标附近的节点,从特定条形图中删除值

Tikz 条形图,坐标附近的节点,从特定条形图中删除值

我用它pgfplots来显示堆叠条形图。对于每个 x 值,堆叠两个条形。我需要在每个条形内显示 y 值,并在顶部显示它们的总和。

我设法实现了这一点,但是第一个条太短,无法正确显示内部值。仅限第一栏,我想只显示顶部的总和,同时隐藏里面的值。

我不知道如何仅针对第一个小节做出这样的例外。

条形图

这是代码:

\begin{tikzpicture}
    \pgfplotsset{
        selective show sum on top/.style={
            /pgfplots/scatter/@post marker code/.append code={%
                \ifnum\coordindex=#1
                   \node[
                   at={(normalized axis cs:%
                       \pgfkeysvalueof{/data point/x},%
                       \pgfkeysvalueof{/data point/y})%
                   },
                   anchor=south,
                   ]
                   {\pgfmathprintnumber{\pgfkeysvalueof{/data point/y}}};
                \fi
            },
        },selective show sum on top/.default=0
    }
    \begin{axis}[%
                ybar stacked,
                ymin=0,
                bar width=24pt,
                xtick=data,
                nodes near coords,
                ylabel=Run time (s),
                xlabel=$T$,
                major x tick style = transparent,
                enlarge x limits=0.15,
                legend cell align={left},
                legend style={
                    at={(0.02,0.885)},
                    anchor=west,
                    column sep=1ex
                }
            ]
        \addplot [fill=blue!60] coordinates {
            (0,2)
            (1,150)
            (2,300)
            (3,450)
        };
        \addplot [fill=red!60,selective show sum on top/.list={0,1,2,3}] coordinates {
            (0,1)
            (1,75)
            (2,150)
            (3,225)
        };
        \legend{Computation,Communication}
    \end{axis}
\end{tikzpicture}

答案1

您可以添加coordinate style/.condition={x==0}{transparent}选项axis,使第一个栏的标签不可见(但它们仍然可以在 PDF 中选择)。

nodes at coords或者,您可以定义一个自定义函数,仅在不是\coordindex时才打印0

\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\begin{document}
\begin{tikzpicture}
    \pgfplotsset{
        selective show sum on top/.style={
            /pgfplots/scatter/@post marker code/.append code={%
                \ifnum\coordindex=#1
                   \node[
                   at={(normalized axis cs:%
                       \pgfkeysvalueof{/data point/x},%
                       \pgfkeysvalueof{/data point/y})%
                   },
                   anchor=south,
                   ]
                   {\pgfmathprintnumber{\pgfkeysvalueof{/data point/y}}};
                \fi
            },
        },
        selective show sum on top/.default=0
    }
    \begin{axis}[%
                ybar stacked,
                ymin=0,
                bar width=24pt,
                xtick=data,
                nodes near coords={
                    \ifnum\coordindex=0\else
                        \pgfmathprintnumber\pgfplotspointmeta
                    \fi
                },
                ylabel=Run time (s),
                xlabel=$T$,
                major x tick style=transparent,
                enlarge x limits=0.15,
                legend cell align={left},
                legend style={
                    at={(0.02,0.885)},
                    anchor=west,
                    column sep=1ex
                },
            ]
        \addplot [fill=blue!60] coordinates {
            (0,2)
            (1,150)
            (2,300)
            (3,450)
        };
        \addplot [fill=red!60, selective show sum on top/.list={0,1,2,3}] coordinates {
            (0,1)
            (1,75)
            (2,150)
            (3,225)
        };
        \legend{Computation, Communication}
    \end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容