- 我有
xbar chart
一个pgfolots
。 - 我想要多个
addplot
命令。 - 我使用
symbolic y coords
和ytick = 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}