我正在尝试使用 更改分组条形图中特定条形的外观TikZ
。但我找不到任何地方如何实现我想要实现的目标。
具体来说,我有三组三个条形图,其中第一个条形图代表与其他两个条形图完全不同的东西。我想让它看起来像我在 powerpoint 中制作的以下示例:
您知道如何实现这一点吗?
编辑:这是我迄今为止所做的:
\documentclass[10pt,bigtitle,twocolumn,SECTION,absheading]{article}
\usepackage{pgfplots,pgfplotstable}
\usetikzlibrary{pgfplots.groupplots}
\begin{document}
\begin{figure}[h]
\pgfplotsset{compat=1.14}
\pgfplotsset{
legend image with text/.style={
legend image code/.code={%
\node[anchor=center] at (0.3cm,0cm) {#1};
}
},
}
\begin{tikzpicture}
\begin{groupplot}[
group style={
group size=1 by 2,
x descriptions at=edge bottom,
y descriptions at=edge left,
vertical sep=1.6mm, % modified
group name=G % added
},
ybar,
axis lines=left, % added
enlarge y limits=0,
enlarge x limits=0.3,
ylabel={\# trials},
xtick=data,
symbolic x coords={gHHT,gATH,gCTR},
nodes near coords,
nodes near coords align={vertical},
every node near coord/.append style={rotate=90, anchor=west},
legend style={at={(0.93,0.9)}},
]
\nextgroupplot[ymin=0, ymax=50, height=6cm, width=8cm]
\addplot coordinates {(gHHT,13.67) (gATH,7) (gCTR,13.5)};
\addplot coordinates {(gHHT,4.67) (gATH,33.6) (gCTR,38)};
\nextgroupplot[ymin=0, ymax=23, height=6cm, width=8cm]
\addplot coordinates {(gHHT,16.4) (gATH,12.3) (gCTR,0)};
\addplot coordinates {(gHHT,7) (gATH,6) (gCTR,0)};
\legend{\# valid,\# failed} % added
\addlegendimage{legend image with text=($\cdot$) \; \,}
\addlegendentry{\# part.}
\end{groupplot}
\node [rotate=90] at (G c1r1.east) {\textit{Manual control}}; % added
\node [rotate=90] at (G c1r2.east) {\textit{Shared control}}; % added
\node at (1.2,2.4) {(3)};
\node at (3.2,4) {(3)};
\node at (5.2,4.25) {(4)};
\node at (1.2,-0.4) {(5)};
\node at (3.2,-01.2) {(3)};
\node at (5.2,-3.8) {(0)};
\draw (current bounding box.south west) rectangle (current bounding box.north east); % added
\end{tikzpicture}
\caption{caption.}
\end{figure}
\end{document}
答案1
您指的是下面这样的代码吗?如果是这样,请查看代码中的注释以了解其工作原理的更多详细信息。
% used PGFPlots v1.14
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots,pgfplotstable}
\usetikzlibrary{pgfplots.groupplots}
\pgfplotsset{
% use this `compat' level or higher
% (it is only needed for the "advanced" positioning of the axis labels)
compat=1.3,
% create a style to hold the stuff needed for a `ycomb' in a `ybar'
% environment
Ycomb/.style={
ycomb,
% because the "bar cycle list" does not contain markers --> add one
mark=*,
% apply the "bar shift" also here ...
xshift=-\pgfplotbarwidth,
% ... and that needs correction/compensation for the `nodes near coords'
nodes near coords style={
yshift=-\pgfplotbarwidth,
},
% because also the `nodes near coords' need/have special labels
% --> allow that here
point meta=explicit symbolic,
% unfortunately the above `xshift' also applies to the legend image
% so this needs to be corrected again
% --> modified default style to correct/compensate that
my ybar legend,
},
% copied `ybar legend' style from the manual
% and modified to compensate the needed `xshift' for the `ycomb' plot
/pgfplots/my ybar legend/.style={
/pgfplots/legend image code/.code={
\draw [##1,
/tikz/.cd,
bar width=3pt,
yshift=-0.2em,
bar shift=0pt,
% compensate `xshift' of `ycomb' plot
% (which also is applied to the legend image)
xshift=10pt,
] plot coordinates {(0cm,0.8em) (2*\pgfplotbarwidth,0.6em)};
},
},
% copied from the manual and modified it, so the shift applies to *all*
% added plots and not only bar plots
/pgfplots/bar shift auto/.style={
/pgf/bar shift={
% total width = n*w + (n-1)*skip
% -> subtract half for centering
-0.5*(\numplots*\pgfplotbarwidth + (\numplots-1)*(#1)) +
% the '0.5*w' is for centering
(.5+\plotnum)*\pgfplotbarwidth + \plotnum*(#1)
},
},
}
\begin{document}
\begin{tikzpicture}
\begin{groupplot}[
group style={
group size=1 by 2,
x descriptions at=edge bottom,
y descriptions at=edge left,
vertical sep=1.6mm,
group name=G,
},
ybar,
axis lines=left,
enlarge y limits=0,
enlarge x limits=0.3,
ylabel={\# trials},
xtick=data,
symbolic x coords={gHHT,gATH,gCTR},
nodes near coords,
nodes near coords align={vertical},
every node near coord/.append style={rotate=90, anchor=west},
legend style={at={(0.93,0.9)}},
]
\nextgroupplot[ymin=0, ymax=50, height=6cm, width=8cm]
% copied next `\addplot' command and applied style
% also added the "custom" labels (shown by the `nodes near coords')
\addplot+ [Ycomb]
coordinates {(gHHT,13.67) [(3)] (gATH,7) [(3)] (gCTR,13.5) [(4)]};
\addplot coordinates {(gHHT,13.67) (gATH,7) (gCTR,13.5)};
\addplot coordinates {(gHHT,4.67) (gATH,33.6) (gCTR,38)};
\nextgroupplot[ymin=0, ymax=23, height=6cm, width=8cm]
% copied next `\addplot' command and applied style
% also added the "custom" labels (shown by the `nodes near coords')
\addplot+ [Ycomb]
coordinates {(gHHT,16.4) [(5)] (gATH,12.3) [(3)] (gCTR,0) [(0)]};
\addplot coordinates {(gHHT,16.4) (gATH,12.3) (gCTR,0)};
\addplot coordinates {(gHHT,7) (gATH,6) (gCTR,0)};
\legend{comb,\# valid,\# failed}
% % the following two lines caused an error, so I just commented them
% \addlegendimage{legend image with text=($\cdot$) \; \,}
% \addlegendentry{\# part.}
\end{groupplot}
\node [rotate=90] at (G c1r1.east) {\textit{Manual control}};
\node [rotate=90] at (G c1r2.east) {\textit{Shared control}};
% I enlarged the bounding box a bit at the top, so the rectangle doesn't
% touch the arrow tip of the upper y-axis any more
% (--> added `yshift')
\draw (current bounding box.south west)
rectangle ([yshift=1ex] current bounding box.north east);
\end{tikzpicture}
\end{document}