在 pgfplot 中对齐图

在 pgfplot 中对齐图

我读过这两本书在 pgfplots 中使用 groupplot 进行图对齐和这个使用 pgfplots,在图之间添加垂直/水平间距?但都没有帮助我。我试图在 pgfplot 中水平对齐图。以下是代码:

\documentclass{article} 
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usetikzlibrary{patterns}
\newcommand{\benchmark}{\textsf}
\usepgfplotslibrary{groupplots}
\usepackage{tikz}
\begin{document}

\pgfplotstableread{
1   north   south
a   0.001280    16.766500
b   34.134100   54.436300
c   33.406100   54.002900
d   16.179600   47.049400
e   35.584100   49.673600
f   60.769900   33.283100
g   34.033700   53.008600
h   34.572000   50.658500
i   30.651700   56.141900
j   26.295600   26.996700
t   40.039200   35.579900
}\africa

\pgfplotstableread{
2   north    south
a   55.417400   44.540100
b   36.533900   55.810100
c   22.362600   76.587400
d   92.259800   5.152260
e   50.970100   48.105000
f   51.881900   38.708600
g   89.573900   9.147390
h       32.110000   54.245900
i   40.787100   52.661700
j   17.979700   70.260900
x   27.225800   51.346500
}\europe

\pgfplotstableread{
3   north   south
a   61.048100   38.920000
b   41.228300   33.868400
c   25.102600   67.448500
d   52.942500   46.574600
e   59.920200   40.050800
f   49.975200   41.445900
g   58.469200   41.373000
h   26.563400   48.328000
i   23.641900   54.914700
j   58.528300   41.439700
y   36.577600   42.945300
}\australia

\pgfplotstableread{
4   north   south
a   16.671400   66.676200
b   15.373900   69.806300
c   26.460400   60.564900
d   20.170900   75.096100
e   44.496400   54.911900
f   40.571300   48.995000
g   28.882700   43.026900
h   39.763300   32.143500
i   33.911500   58.649300
j   22.915300   68.710700
z   26.765400   62.534000
}\latin

\begin{figure}
\begin{minipage}{.45\columnwidth}
\centering
\begin{tikzpicture}[scale = .5, transform shape]    
\begin{groupplot}[
  group style={
   rows=2,
   columns=2,
   horizontal sep=0pt,
   vertical sep=60pt,
   yticklabels at=edge left
 },
 symbolic x coords={a,b,c,d,e,f,g,h,i,j,t},
 xtick=data,
 xticklabel = {\benchmark{\tick}},
 xtick align=outside,
 ytick align=inside,
 x tick label style={rotate=45,anchor=east,font=\Large},
 y tick label style={font=\Large},
 title style={font=\Large},
  ybar stacked,
    ymin=0, ymax=100,
  yticklabel={ $\pgfmathprintnumber{\tick}\,\%$ },
 ]
  \nextgroupplot[title=1,ytick pos=both,xtick pos=left,cycle list={    { fill=olive},                {fill=lightgray,pattern=north east lines} }
]
  \addplot table [x index=0, y index =1] {\africa};
  \addplot table [x index=0, y index =2] {\africa};
\end{groupplot}
\end{tikzpicture}
\end{minipage}
\begin{minipage}{.45\columnwidth}
\centering
\begin{tikzpicture}[scale = .5, transform shape]
\begin{groupplot}[
  group style={
   rows=2,
   columns=2,
   horizontal sep=0pt,
   vertical sep=60pt,
   yticklabels at=edge left
 },
 symbolic x coords={a,b,c,d,e,f,g,h,i,j,x},
 xtick=data,
 xticklabel = {\benchmark{\tick}},
 xtick align=outside,
 ytick align=inside,
 x tick label style={rotate=45,anchor=east,font=\Large},
 y tick label style={font=\Large},
 title style={font=\Large},
  ybar stacked,
    ymin=0, ymax=100,
  yticklabel={ $\pgfmathprintnumber{\tick}\,\%$ },
 ]
\nextgroupplot[title=2,ytick pos=both,xtick pos=left,cycle list={    { fill=olive},        {fill=lightgray,pattern=north east lines}}]
  \addplot table [x index=0, y index =1] {\europe};
  \addplot table [x index=0, y index =2] {\europe};
 \end{groupplot}
\end{tikzpicture}
\end{minipage}
\begin{minipage}{.45\columnwidth}
\centering
\begin{tikzpicture}[scale = .5, transform shape]
\begin{groupplot}[
  group style={
   rows=2,
   columns=2,
   horizontal sep=0pt,
   vertical sep=60pt,
   yticklabels at=edge left
 },
 symbolic x coords={a,b,c,d,e,f,g,h,i,j,y},
 xtick=data,
 xticklabel = {\benchmark{\tick}},
 xtick align=outside,
 ytick align=inside,
 x tick label style={rotate=45,anchor=east,font=\Large},
 y tick label style={font=\Large},
 title style={font=\Large},
  ybar stacked,
    ymin=0, ymax=100,
  yticklabel={ $\pgfmathprintnumber{\tick}\,\%$ },
 ]
\nextgroupplot[title=3,ytick pos=both,xtick pos=left,cycle list={    { fill=olive},        {fill=lightgray,pattern=north east lines}}]
  \addplot table [x index=0, y index =1] {\australia};
  \addplot table [x index=0, y index =2] {\australia};
 \end{groupplot}
\end{tikzpicture}
\end{minipage}
\begin{minipage}{.45\columnwidth}
\centering
\begin{tikzpicture}[scale = .5, transform shape]
\begin{groupplot}[
  group style={
   rows=2,
   columns=2,
   horizontal sep=0pt,
   vertical sep=60pt,
   yticklabels at=edge left
 },
 symbolic x coords={a,b,c,d,e,f,g,h,i,j,z},
 xtick=data,
 xticklabel = {\benchmark{\tick}},
 xtick align=outside,
 ytick align=inside,
 x tick label style={rotate=45,anchor=east,font=\Large},
 y tick label style={font=\Large},
 title style={font=\Large},
     ybar stacked,
    ymin=0, ymax=100,
  yticklabel={ $\pgfmathprintnumber{\tick}\,\%$ },
 ]
\nextgroupplot[title=4,ytick pos=both,xtick pos=left,cycle list={    { fill=olive},    {fill=lightgray,pattern=north east lines}}]
  \addplot table [x index=0, y index =1] {\latin};
  \addplot table [x index=0, y index =2] {\latin};

\end{groupplot}
\end{tikzpicture}
\end{minipage}
\end{figure}
\end{document}

它产生了以下情节:在此处输入图片描述

我想对齐图 4。但是,我甚至不确定哪一个(图 2 还是图 4)没有正确对齐。

我也尝试过避免群体图:

\documentclass{article} 
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usetikzlibrary{patterns}
\newcommand{\benchmark}{\textsf}
\usepgfplotslibrary{groupplots}
\usepackage{tikz}

\begin{document}

\pgfplotstableread{
1   north   south
a   0.001280    16.766500
b   34.134100   54.436300
c   33.406100   54.002900
d   16.179600   47.049400
e   35.584100   49.673600
f   60.769900   33.283100
g   34.033700   53.008600
h   34.572000   50.658500
i   30.651700   56.141900
j   26.295600   26.996700
t   40.039200   35.579900
}\africa

\pgfplotstableread{
2   north    south
a   55.417400   44.540100
b   36.533900   55.810100
c   22.362600   76.587400
d   92.259800   5.152260
e   50.970100   48.105000
f   51.881900   38.708600
g   89.573900   9.147390
h   32.110000   54.245900
i   40.787100   52.661700
j   17.979700   70.260900
x   27.225800   51.346500
}\europe

\pgfplotstableread{
3   north   south
a   61.048100   38.920000
b   41.228300   33.868400
c   25.102600   67.448500
d   52.942500   46.574600
e   59.920200   40.050800
f   49.975200   41.445900
g   58.469200   41.373000
h   26.563400   48.328000
i   23.641900   54.914700
j   58.528300   41.439700
y   36.577600   42.945300
}\australia

\pgfplotstableread{
4   north   south
a   16.671400   66.676200
b   15.373900   69.806300
c   26.460400   60.564900
d   20.170900   75.096100
e   44.496400   54.911900
f   40.571300   48.995000
g   28.882700   43.026900
h   39.763300   32.143500
i   33.911500   58.649300
j   22.915300   68.710700
z   26.765400   62.534000
}\latin


\begin{figure}
\begin{minipage}{.45\columnwidth}
\centering
\begin{tikzpicture}[scale = .5, transform shape]
\begin{axis}[
scale only axis,
 symbolic x coords={a,b,c,d,e,f,g,h,i,j,t},
 xtick=data,
 xticklabel = {\benchmark{\tick}},
 xtick align=outside,
 ytick align=inside,
 x tick label style={rotate=45,anchor=east,font=\Large},
 y tick label style={font=\Large},
 title style={font=\Large},
  ybar stacked,
    ymin=0, ymax=100,
  yticklabel={ $\pgfmathprintnumber{\tick}\,\%$ },
  cycle list = {
   { fill = gray },
   { fill = darkgray,pattern=north east lines },
 }, title={1},
 ]

  \addplot table [x index=0, y index =1] {\africa};
  \addplot table [, x index=0, y index =2] {\africa};
\end{axis}
\end{tikzpicture}
\end{minipage}
\begin{minipage}{.45\columnwidth}
\centering
\begin{tikzpicture}[scale = .5, transform shape]
\begin{axis}[
scale only axis,
 symbolic x coords={a,b,c,d,e,f,g,h,i,j,x},
 xtick=data,
 xticklabel = {\benchmark{\tick}},
 xtick align=outside,
 ytick align=inside,
 x tick label style={rotate=45,anchor=east,font=\Large},
 y tick label style={font=\Large},
 title style={font=\Large},
  ybar stacked,
    ymin=0, ymax=100,
  yticklabel={ $\pgfmathprintnumber{\tick}\,\%$ },
  cycle list = {
   { fill = gray },
   { fill = darkgray,pattern=north east lines },
 }, title={2},
 ]

  \addplot table [x index=0, y index =1,fill=olive] {\europe};
  \addplot table [, x index=0, y index =2,fill=lightgray] {\europe};
\end{axis}
\end{tikzpicture}
\end{minipage}
\begin{minipage}{.45\columnwidth}
\centering
\begin{tikzpicture}[scale = .5, transform shape]
\begin{axis}[
scale only axis,
 symbolic x coords={a,b,c,d,e,f,g,h,i,j,y},
 xtick=data,
 xticklabel = {\benchmark{\tick}},
 xtick align=outside,
 ytick align=inside,
 x tick label style={rotate=45,anchor=east,font=\Large},
 y tick label style={font=\Large},
 title style={font=\Large},
  ybar stacked,
    ymin=0, ymax=100,
  yticklabel={ $\pgfmathprintnumber{\tick}\,\%$ },
  cycle list = {
   { fill = gray },
   { fill = darkgray,pattern=north east lines },
 }, title={3},
 ]

  \addplot table [x index=0, y index =1,fill=olive] {\australia};
  \addplot table [, x index=0, y index =2,fill=lightgray] {\australia};
\end{axis}
\end{tikzpicture}
\end{minipage}
\begin{minipage}{.45\columnwidth}
\centering
\begin{tikzpicture}[scale = .5, transform shape]
\begin{axis}[
scale only axis,
 symbolic x coords={a,b,c,d,e,f,g,h,i,j,z},
 xtick=data,
 xticklabel = {\benchmark{\tick}},
 xtick align=outside,
 ytick align=inside,
 x tick label style={rotate=45,anchor=east,font=\Large},
 y tick label style={font=\Large},
 title style={font=\Large},
  ybar stacked,
    ymin=0, ymax=100,
  yticklabel={ $\pgfmathprintnumber{\tick}\,\%$ },
  cycle list = {
   { fill = gray },
   { fill = darkgray,pattern=north east lines },
 }, title={4},
 ]

  \addplot table [x index=0, y index =1,fill=olive] {\latin};
  \addplot table [, x index=0, y index =2,fill=lightgray] {\latin};
\end{axis}
\end{tikzpicture}
\end{minipage}
\end{figure}
\end{document}

其结果类似如下:在此处输入图片描述

我该如何解决?最好使用 groupplots 解决方案,因为我有很多,并且不想更改它。

答案1

我认为您使用的groupplots并不完全符合预期。使用groupplot您指定的选项,并稍微更改代码:

在此处输入图片描述

关键是,您只有一个groupplot环境,并且在其中您用 开始一个新轴\nextgroupplot

\documentclass{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usetikzlibrary{patterns}
\newcommand{\benchmark}{\textsf}
\usepgfplotslibrary{groupplots}
\usepackage{tikz}
\begin{document}

\pgfplotstableread{
1   north   south
a   0.001280    16.766500
b   34.134100   54.436300
c   33.406100   54.002900
d   16.179600   47.049400
e   35.584100   49.673600
f   60.769900   33.283100
g   34.033700   53.008600
h   34.572000   50.658500
i   30.651700   56.141900
j   26.295600   26.996700
t   40.039200   35.579900
}\africa

\pgfplotstableread{
2   north    south
a   55.417400   44.540100
b   36.533900   55.810100
c   22.362600   76.587400
d   92.259800   5.152260
e   50.970100   48.105000
f   51.881900   38.708600
g   89.573900   9.147390
h       32.110000   54.245900
i   40.787100   52.661700
j   17.979700   70.260900
x   27.225800   51.346500
}\europe

\pgfplotstableread{
3   north   south
a   61.048100   38.920000
b   41.228300   33.868400
c   25.102600   67.448500
d   52.942500   46.574600
e   59.920200   40.050800
f   49.975200   41.445900
g   58.469200   41.373000
h   26.563400   48.328000
i   23.641900   54.914700
j   58.528300   41.439700
y   36.577600   42.945300
}\australia

\pgfplotstableread{
4   north   south
a   16.671400   66.676200
b   15.373900   69.806300
c   26.460400   60.564900
d   20.170900   75.096100
e   44.496400   54.911900
f   40.571300   48.995000
g   28.882700   43.026900
h   39.763300   32.143500
i   33.911500   58.649300
j   22.915300   68.710700
z   26.765400   62.534000
}\latin

\begin{figure}
\begin{minipage}{.95\columnwidth}
\centering
\begin{tikzpicture}[scale = .5, transform shape]    
\begin{groupplot}[
  group style={
   rows=2,
   columns=2,
   horizontal sep=0pt,
   vertical sep=60pt,
   yticklabels at=edge left
 },
 xtick=data,
 xticklabel = {\benchmark{\tick}},
 xtick align=outside,
 ytick align=inside,
 x tick label style={rotate=45,anchor=east,font=\Large},
 y tick label style={font=\Large},
 title style={font=\Large},
  ybar stacked,
    ymin=0, ymax=100,
  yticklabel={ $\pgfmathprintnumber{\tick}\,\%$ },
 ]
  \nextgroupplot[title=1,ytick pos=both,xtick pos=left,cycle list={    { fill=olive},                {fill=lightgray,pattern=north east lines} }
 ,symbolic x coords={a,b,c,d,e,f,g,h,i,j,t}]
  \addplot table [x index=0, y index =1] {\africa};
  \addplot table [x index=0, y index =2] {\africa};

\nextgroupplot[title=2,ytick pos=both,xtick pos=left,cycle list={    { fill=olive},        {fill=lightgray,pattern=north east lines}},
 symbolic x coords={a,b,c,d,e,f,g,h,i,j,x}]
  \addplot table [x index=0, y index =1] {\europe};
  \addplot table [x index=0, y index =2] {\europe};

\nextgroupplot[title=3,ytick pos=both,xtick pos=left,cycle list={    { fill=olive},        {fill=lightgray,pattern=north east lines}},
symbolic x coords={a,b,c,d,e,f,g,h,i,j,y}]
  \addplot table [x index=0, y index =1] {\australia};
  \addplot table [x index=0, y index =2] {\australia};

\nextgroupplot[title=4,ytick pos=both,xtick pos=left,cycle list={    { fill=olive},    {fill=lightgray,pattern=north east lines}},
symbolic x coords={a,b,c,d,e,f,g,h,i,j,z}]
  \addplot table [x index=0, y index =1] {\latin};
  \addplot table [x index=0, y index =2] {\latin};

\end{groupplot}
\end{tikzpicture}
\end{minipage}
\end{figure}
\end{document}

答案2

添加\centering之后就\begin{figure}可以解决这个问题。


对于你的大量情节:

添加

\makeatletter
\g@addto@macro\@floatboxreset\centering
\makeatother

在您的序言中添加此代码将自动为所有浮动内容(图形、表格)执行此操作。您再也不会忘记它,也不必每次都输入它。对于某些环境(子标题的子图形以及迷你页面,我猜也是如此),这不起作用。不知道为什么。但它完全解决了您的问题。

答案3

或者您可以将图表粘贴到表格中。

\documentclass{article} 
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usetikzlibrary{patterns}
\newcommand{\benchmark}{\textsf}
\usepgfplotslibrary{groupplots}
\usepackage{tikz}
\begin{document}

\pgfplotstableread{
1   north   south
a   0.001280    16.766500
b   34.134100   54.436300
c   33.406100   54.002900
d   16.179600   47.049400
e   35.584100   49.673600
f   60.769900   33.283100
g   34.033700   53.008600
h   34.572000   50.658500
i   30.651700   56.141900
j   26.295600   26.996700
t   40.039200   35.579900
}\africa

\pgfplotstableread{
2   north    south
a   55.417400   44.540100
b   36.533900   55.810100
c   22.362600   76.587400
d   92.259800   5.152260
e   50.970100   48.105000
f   51.881900   38.708600
g   89.573900   9.147390
h       32.110000   54.245900
i   40.787100   52.661700
j   17.979700   70.260900
x   27.225800   51.346500
}\europe

\pgfplotstableread{
3   north   south
a   61.048100   38.920000
b   41.228300   33.868400
c   25.102600   67.448500
d   52.942500   46.574600
e   59.920200   40.050800
f   49.975200   41.445900
g   58.469200   41.373000
h   26.563400   48.328000
i   23.641900   54.914700
j   58.528300   41.439700
y   36.577600   42.945300
}\australia

\pgfplotstableread{
4   north   south
a   16.671400   66.676200
b   15.373900   69.806300
c   26.460400   60.564900
d   20.170900   75.096100
e   44.496400   54.911900
f   40.571300   48.995000
g   28.882700   43.026900
h   39.763300   32.143500
i   33.911500   58.649300
j   22.915300   68.710700
z   26.765400   62.534000
}\latin


\begin{tabular}{cc}
\begin{tikzpicture}[scale = .5, transform shape]    
\begin{groupplot}[
  group style={
   rows=2,
   columns=2,
   horizontal sep=0pt,
   vertical sep=60pt,
   yticklabels at=edge left
 },
 symbolic x coords={a,b,c,d,e,f,g,h,i,j,t},
 xtick=data,
 xticklabel = {\benchmark{\tick}},
 xtick align=outside,
 ytick align=inside,
 x tick label style={rotate=45,anchor=east,font=\Large},
 y tick label style={font=\Large},
 title style={font=\Large},
  ybar stacked,
    ymin=0, ymax=100,
  yticklabel={ $\pgfmathprintnumber{\tick}\,\%$ },
 ]
  \nextgroupplot[title=1,ytick pos=both,xtick pos=left,cycle list={    { fill=olive},                {fill=lightgray,pattern=north east lines} }
]
  \addplot table [x index=0, y index =1] {\africa};
  \addplot table [x index=0, y index =2] {\africa};
\end{groupplot}
\end{tikzpicture}
&
\begin{tikzpicture}[scale = .5, transform shape]
\begin{groupplot}[
  group style={
   rows=2,
   columns=2,
   horizontal sep=0pt,
   vertical sep=60pt,
   yticklabels at=edge left
 },
 symbolic x coords={a,b,c,d,e,f,g,h,i,j,x},
 xtick=data,
 xticklabel = {\benchmark{\tick}},
 xtick align=outside,
 ytick align=inside,
 x tick label style={rotate=45,anchor=east,font=\Large},
 y tick label style={font=\Large},
 title style={font=\Large},
  ybar stacked,
    ymin=0, ymax=100,
  yticklabel={ $\pgfmathprintnumber{\tick}\,\%$ },
 ]
\nextgroupplot[title=2,ytick pos=both,xtick pos=left,cycle list={    { fill=olive},        {fill=lightgray,pattern=north east lines}}]
  \addplot table [x index=0, y index =1] {\europe};
  \addplot table [x index=0, y index =2] {\europe};
 \end{groupplot}
\end{tikzpicture}
\\
\begin{tikzpicture}[scale = .5, transform shape]
\begin{groupplot}[
  group style={
   rows=2,
   columns=2,
   horizontal sep=0pt,
   vertical sep=60pt,
   yticklabels at=edge left
 },
 symbolic x coords={a,b,c,d,e,f,g,h,i,j,y},
 xtick=data,
 xticklabel = {\benchmark{\tick}},
 xtick align=outside,
 ytick align=inside,
 x tick label style={rotate=45,anchor=east,font=\Large},
 y tick label style={font=\Large},
 title style={font=\Large},
  ybar stacked,
    ymin=0, ymax=100,
  yticklabel={ $\pgfmathprintnumber{\tick}\,\%$ },
 ]
\nextgroupplot[title=3,ytick pos=both,xtick pos=left,cycle list={    { fill=olive},        {fill=lightgray,pattern=north east lines}}]
  \addplot table [x index=0, y index =1] {\australia};
  \addplot table [x index=0, y index =2] {\australia};
 \end{groupplot}
\end{tikzpicture}
&
\begin{tikzpicture}[scale = .5, transform shape]
\begin{groupplot}[
  group style={
   rows=2,
   columns=2,
   horizontal sep=0pt,
   vertical sep=60pt,
   yticklabels at=edge left
 },
 symbolic x coords={a,b,c,d,e,f,g,h,i,j,z},
 xtick=data,
 xticklabel = {\benchmark{\tick}},
 xtick align=outside,
 ytick align=inside,
 x tick label style={rotate=45,anchor=east,font=\Large},
 y tick label style={font=\Large},
 title style={font=\Large},
     ybar stacked,
    ymin=0, ymax=100,
  yticklabel={ $\pgfmathprintnumber{\tick}\,\%$ },
 ]
\nextgroupplot[title=4,ytick pos=both,xtick pos=left,cycle list={    { fill=olive},    {fill=lightgray,pattern=north east lines}}]
  \addplot table [x index=0, y index =1] {\latin};
  \addplot table [x index=0, y index =2] {\latin};

\end{groupplot}
\end{tikzpicture}
\end{tabular}
\end{document}

在此处输入图片描述

相关内容