tikz xbar 额外坐标

tikz xbar 额外坐标

我正在尝试绘制数字的比较

\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        xbar,
        y axis line style = { opacity = 0 },
        axis x line       = none,
        tickwidth         = 0pt,
        enlarge y limits  = 0.2,
        enlarge x limits  = 0.02,
        symbolic y coords = {SRE, SWB},
        nodes near coords,
    ]
        \addplot coordinates { (36470,SRE)     (33039,SWB) };
        \addplot coordinates { (30460,SRE)     (25876,SWB) };
        \addplot coordinates { (25640,SRE)     (21761,SWB) };
        \legend{Combined, 2phnn, 1phnn}
    \end{axis}
\end{tikzpicture}
\end{document}

但我目前得到的结果是这样的

错误的结果

我想移动标签,因为它隐藏了一些东西,我不明白为什么会出现一个额外的“SWB”

答案1

正如 esdd 在评论中提到的那样,添加ytick=data只会在指定的数据值处添加刻度。要移动图例,请legend style={at={(rel axis cs:1,0.5)},anchor=east}axis选项中添加类似的内容。

在此处输入图片描述

\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        xbar,
        y axis line style = { opacity = 0 },
        axis x line       = none,
        tickwidth         = 0pt,
        enlarge y limits  = 0.2,
        enlarge x limits  = 0.02,
        symbolic y coords = {SRE, SWB},
        nodes near coords,
        ytick=data,
        legend style={at={(rel axis cs:1,0.5)},anchor=east}
    ]
        \addplot coordinates { (36470,SRE)     (33039,SWB) };
        \addplot coordinates { (30460,SRE)     (25876,SWB) };
        \addplot coordinates { (25640,SRE)     (21761,SWB) };
        \legend{Combined, 2phnn, 1phnn}
    \end{axis}
\end{tikzpicture}
\end{document}

以下是空白较少的变体:

在此处输入图片描述

\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        xbar,
        height=5cm,width=8cm, % added
        y axis line style = { opacity = 0 },
        axis x line       = none,
        tickwidth         = 0pt,
        enlarge y limits  = 0.7, % modified
        enlarge x limits  = 0.02,
        symbolic y coords = {SRE, SWB},
        nodes near coords,
        ytick=data,
         legend style={
            at={(rel axis cs:0.5,1)},
            anchor=south,
            legend columns=-1, % -1 means place all in a row
            column sep=2mm, % more space
            draw=none % removes frame
            }
    ]
        \addplot coordinates { (36470,SRE)     (33039,SWB) };
        \addplot coordinates { (30460,SRE)     (25876,SWB) };
        \addplot coordinates { (25640,SRE)     (21761,SWB) };
        \legend{Combined, 2phnn, 1phnn}
    \end{axis}
\end{tikzpicture}
\end{document}

相关内容