带有符号坐标的条形图条形上的自定义标签

带有符号坐标的条形图条形上的自定义标签

我想在这个图表的条形上添加标签,但我想要的标签既不是条形的 x 坐标也不是 y 坐标,而是其他文本。似乎我应该能够使用 来做到这一点\node at (test\_1) {label},但我尝试编译时出现错误。

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{width=15cm,compat=1.9}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
    title={Benchmark Speed Comparison of Cache Sizes},
    xlabel={Benchmark},
    ylabel={Benchmark Time [ns]},
    major x tick style=transparent,
    ybar=2*\pgflinewidth,
    bar width=6pt,
    x tick label style={rotate=45, anchor=east},
    symbolic x coords={test\_1, test\_2},
    ymax=500000000,
    xtick=data,
    ytick={100,1000,10000,100000,100000,1000000,10000000,100000000},
    legend pos= outer north east,
    ymajorgrids=true,
    grid style=dashed,
    ymode=log,
    log basis y={10},
    enlarge x limits=0.25,
]

\addplot[fill=red] coordinates { (test\_1,775) (test\_2,5597) };

\addplot[fill=blue] coordinates { (test\_1,112) (test\_2,112) };

\end{axis}
\end{tikzpicture}
\end{document}

渲染图表

答案1

我不确定我是否明白你想要做什么。

nodes near coords,
point meta=explicit symbolic

可以为多个条形图设置明确的标签

\addplot[fill=red] coordinates { 
  (test\_1,775)[\textcolor{red}{label}]
  (test\_2,5597)
};

在此处输入图片描述

代码:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{width=15cm,compat=1.9}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
    title={Benchmark Speed Comparison of Cache Sizes},
    xlabel={Benchmark},
    ylabel={Benchmark Time [ns]},
    major x tick style=transparent,
    ybar=2*\pgflinewidth,
    bar width=6pt,
    x tick label style={rotate=45, anchor=east},
    symbolic x coords={test\_1, test\_2},
    ymax=500000000,
    xtick=data,
    ytick={100,1000,10000,100000,100000,1000000,10000000,100000000},
    legend pos= outer north east,
    ymajorgrids=true,
    grid style=dashed,
    ymode=log,
    log basis y={10},
    enlarge x limits=0.25,
    nodes near coords,
    point meta=explicit symbolic
]

\addplot[fill=red] coordinates { 
  (test\_1,775)[\textcolor{red}{label}]
  (test\_2,5597)
};

\addplot[fill=blue] coordinates {
  (test\_1,112)
  (test\_2,112)[\Large\textcolor{blue}{$\star$}]
};
\end{axis}
\end{tikzpicture}
\end{document}

如果您想手动设置标签,请不要在符号坐标内使用反斜杠。请改用

symbolic x coords={test_1, test_2},
xticklabels={test\_1,test\_2},

\addplot[fill=red] coordinates { (test_1,775) (test_2,5597) };
\addplot[fill=blue] coordinates { (test_1,112) (test_2,112) };

然后可以在符号 x 坐标处设置自己的标签test_1

\node at (axis cs:test_1,2000){\textcolor{red}{label}};

请注意,您必须使用axis cs:

在此处输入图片描述

代码:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{width=15cm,compat=1.9}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
    title={Benchmark Speed Comparison of Cache Sizes},
    xlabel={Benchmark},
    ylabel={Benchmark Time [ns]},
    major x tick style=transparent,
    ybar=2*\pgflinewidth,
    bar width=6pt,
    x tick label style={rotate=45, anchor=east},
    symbolic x coords={test_1, test_2},
    xticklabels={test\_1,test\_2},
    ymax=500000000,
    xtick=data,
    ytick={100,1000,10000,100000,100000,1000000,10000000,100000000},
    legend pos= outer north east,
    ymajorgrids=true,
    grid style=dashed,
    ymode=log,
    log basis y={10},
    enlarge x limits=0.25,
]

\addplot[fill=red] coordinates { (test_1,775) (test_2,5597) };
\addplot[fill=blue] coordinates { (test_1,112) (test_2,112)};

\node at (axis cs:test_1,2000){\textcolor{red}{label}};

\end{axis}
\end{tikzpicture}
\end{document}

相关内容