我正在尝试创建一个由 2 个独立图表组成的 tikzpicture。我正在使用这个\usepgfplotslibrary{groupplots}
包来实现这一点。一切进展顺利,但我似乎无法将条形图移到更中心的位置(见图:它们目前位于图的最角落)。
这是一个最小的工作示例:
\documentclass{article}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\usepackage{tikz}
\usetikzlibrary{matrix}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
\begin{groupplot}[
group style={group size= 2 by 1},
height=8cm,
width=5cm,
ymax=100,
ymin=0,
ybar,
xtick=data,
x tick label style={rotate=45,anchor=east}
]
\nextgroupplot[title=A,legend to name=zelda, symbolic x coords={Class3, Class4}, bar width=9pt]
\addplot+[ybar, draw=red, fill=red] plot coordinates {
(Class3, 60.83)
(Class4, 54.83)
};
\addlegendentry{(Both) correct};
\addplot+[ybar, draw=blue, fill=blue] plot coordinates {
(Class3, 37.00)
(Class4, 42.17)
};
\addlegendentry{One correct};
\coordinate (top) at (rel axis cs:0,1);% coordinate at top of the first plot
\nextgroupplot[title=B, symbolic x coords={Class1, Class2}, bar width=9pt]
\addplot+[ybar, draw=red, fill=red] plot coordinates {
(Class1,89.00)
(Class2,87.00)
};
\coordinate (bot) at (rel axis cs:1,0);% coordinate at bottom of the last plot
\end{groupplot}
\path (top)--(bot) coordinate[midway] (group center);
\node[above,rotate=90] at (group center -| current bounding box.west) {Accuracy};
\node[right=1em,inner sep=0pt] at(group center -| current bounding box.east {\pgfplotslegendfromname{zelda}};
\end{tikzpicture}
\end{document}
任何帮助都将非常感激。
答案1
事实证明,enlarge x limits
成功了。这使得必须将符号坐标更改为xtick={1,2},
,然后将放大 x 限制 = 0.5 添加到\nextgroupplot
部分
完整代码:
\documentclass{article}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\usepackage{tikz}
\usetikzlibrary{matrix}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
\begin{groupplot}[
group style={group size= 2 by 1},
height=8cm,
width=5cm,
ymax=100,
ymin=0,
ybar,
xtick={1,2},
x tick label style={rotate=45,anchor=east}
]
\nextgroupplot[title=A,legend to name=zelda, xticklabels={Class3, Class4}, bar width=9pt, enlarge x limits=0.5]
\addplot+[ybar, draw=red, fill=red] plot coordinates {
(Class3, 60.83)
(Class4, 54.83)
};
\addlegendentry{(Both) correct};
\addplot+[ybar, draw=blue, fill=blue] plot coordinates {
(Class3, 37.00)
(Class4, 42.17)
};
\addlegendentry{One correct};
\coordinate (top) at (rel axis cs:0,1);% coordinate at top of the first plot
\nextgroupplot[title=B, xticklabels={Class1, Class2}, bar width=9pt, enlarge x limits=0.5]
\addplot+[ybar, draw=red, fill=red] plot coordinates {
(Class1,89.00)
(Class2,87.00)
};
\coordinate (bot) at (rel axis cs:1,0);% coordinate at bottom of the last plot
\end{groupplot}
\path (top)--(bot) coordinate[midway] (group center);
\node[above,rotate=90] at (group center -| current bounding box.west) {Accuracy};
\node[right=1em,inner sep=0pt] at(group center -| current bounding box.east {\pgfplotslegendfromname{zelda}};
\end{tikzpicture}
\end{document}