带有 PGFPlots 的条形图:两个组,每个组有两个类别和两个条形图

带有 PGFPlots 的条形图:两个组,每个组有两个类别和两个条形图

使用\pgfplots,我得到了如下所示的条形图: enter image description here

相反,我希望有类似的东西,这样我就有两个组(“Freie Wiedergabe Tag 1”和“Freie Wiedergabe Tag 2”),分为两个类别(“Erinnert”和“Nicht erinnert”),每个类别有两个条形(“neutral”和“negativ”)。我不知道该怎么做:

enter image description here

我的 MWE 在这里:

\documentclass{apa6}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13}
\usetikzlibrary{arrows.meta}
\begin{document}
\begin{figure}
\begin{tikzpicture}[scale=1.6,transform shape]
\begin{axis}[
ybar,
enlargelimits=0.15,
legend style={at={(0.5,-0.25)},
anchor=north,legend columns=-1},
ylabel={Anteil},
symbolic x coords={Freie Wiedergabe Tag 1,Freie Wiedergabe Tag 2,Treffer,Falscher Alarm},
xtick=data,
xticklabel style={text width=1.5cm,
font=\tiny,
align=center
},
]
%neutral
\addplot[blue,fill=blue!30!white,error bars/.cd,y dir=both,y explicit,]
coordinates{(Treffer,0.8560) +-(0.01503,0.01503) (Falscher Alarm,0.1390) +-(0.01737,0.01737)(Freie Wiedergabe Tag 1,0.1481) +-(0.01067,0.01067) (Freie Wiedergabe Tag 2,0.1119) +-(0.00922,0.00922) };
%negativ
\addplot[red,fill=red!30!white,error bars/.cd,y dir=both,y explicit,]
coordinates {(Treffer,0.9365) +- (0.00587,0.00587)(Falscher Alarm,0.1435) +- (0.01737,0.01737)(Freie Wiedergabe Tag 1,0.3247) +-(0.01695,0.01695) (Freie Wiedergabe Tag 2,0.2556) +-(0.01524,0.01524)
};
\legend{neutral,negativ}
\end{axis}
\end{tikzpicture}
\caption{Unterschrift}
\label{GedaechtnisBilder}
\end{figure}
\end{document}

答案1

数据点可能不在正确的位置,但除此之外,还有一种实现方法如下所示。我将坐标改为数值 x 值 (1,2,3,4),而不是符号坐标,并设置xticklabels={Erinnert,Nicht erinnert,Erinnert,Nicht erinnert}。因此,每个类别中的四个数据点\addplot应对应于这些类别,前两个代表第一天,后两个代表第二天。

然后我添加了两个extra x ticks={1.5,3.5}(在两组条形图的中间),带有extra x tick labels={Freie Wiedergabe Tag 1,Freie Wiedergabe Tag 2}。对刻度标签进行了一些垂直移动。

output of code

\documentclass{apa6}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13}
\usetikzlibrary{arrows.meta}
\begin{document}
\begin{figure}
\begin{tikzpicture}
\begin{axis}[
% instead of scaling
width=0.6\linewidth,
ybar,
enlargelimits=0.15,
legend style={
% to save space I would place the legend inside the axis, at least with these data
  %at={(0.5,-0.2)},
  %anchor=north,
legend columns=-1},
ylabel={Anteil},
ylabel style={font=\large},
xtick=data,
% use explicit ticklabels instead of symbolc x coords
xticklabels={Erinnert,Nicht erinnert,Erinnert,Nicht erinnert},
xticklabel style={
%  text width=2cm,
  yshift=-18pt, % move xticks down a bit
%  align=center
},
% extra ticks
extra x ticks={1.5,3.5},
extra x tick labels={Freie Wiedergabe Tag 1,Freie Wiedergabe Tag 2},
extra x tick style={
  % because the xticklabel style also affects the extra ticks, 
  % shift extra ticklabels back up
  ticklabel style={yshift=13pt},
  % tickwidth is actually the length of of the ticks (the small lines)
  tickwidth=0
}
]
%neutral
\addplot[blue,fill=blue!30!white,error bars/.cd,y dir=both,y explicit,]
coordinates{
   (1,0.8560) +-(0.01503,0.01503)
   (2,0.1390) +-(0.01737,0.01737)
   (3,0.1481) +-(0.01067,0.01067)
   (4,0.1119) +-(0.00922,0.00922)
};
%negativ
\addplot[red,fill=red!30!white,error bars/.cd,y dir=both,y explicit,]
coordinates {
  (1,0.9365) +- (0.00587,0.00587)
  (2,0.1435) +- (0.01737,0.01737)
  (3,0.3247) +-(0.01695,0.01695)
  (4,0.2556) +-(0.01524,0.01524)
};
\legend{neutral,negativ}
\end{axis}
\end{tikzpicture}
\caption{Unterschrift}
\label{GedaechtnisBilder}
\end{figure}
\end{document}

相关内容