pgfplots:条形图 | 多个 addplot | 居中以勾选 | 第二个 addplot 的标签缺失

pgfplots:条形图 | 多个 addplot | 居中以勾选 | 第二个 addplot 的标签缺失
  • 我有xbar chart一个pgfolots
  • 我想要多个addplot命令。
  • 我使用symbolic y coordsytick = data
  • 问题 1我想要全部居中关于ytick
  • 问题 2标签第二addplot丢失的
  • 背景:我想要这种特殊的布局,因为我想nodes near coords在命令的后面使用它addplot(仅针对特定的条)。

\documentclass[border=5mm,tikz]{standalone}

\usepackage{tikz}
\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
    xbar,
    symbolic y coords = {A,B,C,D,E},
    enlarge y limits = 0.15,
    ytick = data,
    width = 160mm,
    height = 90mm,
    xmin = 0,
    xmax = 10,
    title = {One \texttt{\textbackslash addplot}},
    ]
\addplot[fill=green] coordinates {(1,A) (2,B) (3,C) (4,D) (9,E)};
\end{axis}
\end{tikzpicture}

\begin{tikzpicture}
\begin{axis}[
    xbar,
    symbolic y coords = {A,B,C,D,E},
    enlarge y limits = 0.15,
    ytick = data,
    width = 160mm,
    height = 90mm,
    xmin = 0,
    xmax = 10,
    title = {Two \texttt{\textbackslash addplot}},
    ]
\addplot[fill=green] coordinates {(1,A) (2,B)}; % A-B
\addplot[fill=green] coordinates {(3,C) (4,D) (9,E)}; % C-E
\end{axis}
\end{tikzpicture}

\end{document}

在此处输入图片描述


答案1

解决起来很简单...

% used PGFPlots v1.14
\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        width = 160mm,
        height = 120mm,
        xmin = 0,
        xmax = 10,
        enlarge y limits = 0.15,
        symbolic y coords = {A,B,C,D,E},
%        % this only uses the data of the *first* `\addplot' command, but you don't
%        % need it at all in this case
%        ytick=data,
%        % in case the `ytick's haven't a Delta of 1 you could do either use
%        ytick={A,...,E},
        % ... or set the tick distance to 1
        ytick distance=1,
        title = {Two \texttt{\textbackslash addplot} and remove \texttt{ytick = data} but increase \texttt{height = 120mm}},
        xbar,
        bar shift=0pt, % --> added
    ]
        \addplot [fill=green] coordinates {(1,A) (2,B)}; % A-B
        \addplot [fill=green] coordinates {(3,C) (4,D) (9,E)}; % C-E

        % to add some stuff at coordinates in between the symbolic coordinates
        % use the `nomalized' option
        \node [circle,fill=red,inner sep=0pt,minimum size=5pt,pin={$1.5$}]
            at (axis cs:6,{[normalized]1.5}) {};
    \end{axis}
\end{tikzpicture}
\end{document}

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

相关内容