我想将这些组图垂直居中,但我做不到

我想将这些组图垂直居中,但我做不到
\documentclass[border=1cm]{standalone}
\usepackage[x11names,dvipsnames]{xcolor}
\usepackage{tikz}
\usepackage{tikz-3dplot}
\usepackage{pgfplots}

    \usetikzlibrary{arrows,snakes,patterns,decorations.markings,shapes.geometric,angles,quotes,bending,petri,decorations.text,calc,arrows.meta,mindmap,trees}
\usepgfplotslibrary{groupplots,polar} %




\begin{document}    
\definecolor{aqaqaq}{rgb}{0.6274509803921569,0.6274509803921569,0.6274509803921569}
\definecolor{wqwqwq}{rgb}{0.3764705882352941,0.3764705882352941,0.3764705882352941}
\begin{tikzpicture}
\begin{groupplot}[group style={group size=2 by 3},no markers, 
axis y line=center,
axis x line=middle,
enlargelimits=true,
every axis x label/.style={
at={(ticklabel* cs:1)},
anchor=west,
},
every axis y label/.style={
at={(ticklabel* cs:1)},
anchor=south,
}]
\nextgroupplot[width=7cm,height=6cm,xlabel=$x$,ylabel=$\sin x$,xtick={
-6.28318, -4.7123889, -3.14159, -1.5708,
1.5708, 3.14159, 4.7123889, 6.28318,7.853981,9.424777
},
xticklabels={
$-2\pi$, $-\frac{3\pi}{2}$, $-\pi$, $-\frac{\pi}{2}$,
$\frac{\pi}{2}$, $\pi$, $\frac{3\pi}{2}$, $2\pi$, $\frac{5\pi}{2}$, $3\pi$},
]
\addplot[smooth,blue,mark=none,domain=0:2*pi,samples=40]{cos(deg(x))};
\draw[red,dashed,thick] (3,10) rectangle (5,25);
\node (a2) at (axis cs:pi/6,0.5) {};
\node (b2) at (axis cs:pi/3,0.866) {};
\node (c2) at (axis cs:pi/2,1) {};
\node (d2) at (axis cs:3*pi/4,0.7071) {};
\nextgroupplot[height=4cm,axis lines=none,ymin=-1,xmin=-0.5,ymax=2,xmax=9,x=0.75cm, y=0.75cm]
\addplot coordinates{(0,1)};
\draw[->,color=black] (axis cs:-0.5,0.) -- (axis cs:9.5,0.) node[at end,right]{$x$};
\fill[color=wqwqwq,fill=wqwqwq,pattern=north east lines,pattern color=wqwqwq] (axis cs:-0.5,2.) -- (axis cs:0.,2.) -- (axis cs:0.,0.) -- (axis cs:9.,0.) -- (axis cs:9.,-0.5) -- (axis cs:-0.5,-0.5) -- cycle;
\fill[blue!40!white] (axis cs:4.5,0) rectangle (axis cs:5.5,1);
\draw [color=wqwqwq] (axis cs:-0.5,2.)-- (axis cs:0.,2.);
\draw [color=wqwqwq] (axis cs:0.,2.)-- (axis cs:0.,0.);
\draw [color=wqwqwq] (axis cs:0.,0.)-- (axis cs:9.,0.);
\draw [color=wqwqwq] (axis cs:9.,0.)-- (axis cs:9.,-0.5);
\draw [color=wqwqwq] (axis cs:9.,-0.5)-- (axis cs:-0.5,-0.5);
\draw [color=wqwqwq] (axis cs:-0.5,-0.5)-- (axis cs:-0.5,2.);
\draw [line width=1.6pt,color=aqaqaq,decorate,decoration={coil,segment length=6pt,amplitude=3mm}] (axis cs:0.,0.5)-- (axis cs:4.5,0.5);

\nextgroupplot[width=7cm,height=6cm,xlabel=$x$,ylabel=$\sin x$,xtick={
-6.28318, -4.7123889, -3.14159, -1.5708,
1.5708, 3.14159, 4.7123889, 6.28318,7.853981,9.424777
},
xticklabels={
$-2\pi$, $-\frac{3\pi}{2}$, $-\pi$, $-\frac{\pi}{2}$,
$\frac{\pi}{2}$, $\pi$, $\frac{3\pi}{2}$, $2\pi$, $\frac{5\pi}{2}$, $3\pi$},
]
\addplot[smooth,blue,mark=none,domain=0:2*pi,samples=40]{cos(deg(x))};
\draw[red,dashed,thick] (3,10) rectangle (5,25);
\node (a2) at (axis cs:pi/6,0.5) {};
\node (b2) at (axis cs:pi/3,0.866) {};
\node (c2) at (axis cs:pi/2,1) {};
\node (d2) at (axis cs:3*pi/4,0.7071) {};
\nextgroupplot[height=4cm,axis lines=none,ymin=-1,xmin=-0.5,ymax=2,xmax=9,x=0.75cm, y=0.75cm]
\addplot coordinates{(0,1)};
\draw[->,color=black] (axis cs:-0.5,0.) -- (axis cs:9.5,0.) node[at end,right]{$x$};
\fill[color=wqwqwq,fill=wqwqwq,pattern=north east lines,pattern color=wqwqwq] (axis cs:-0.5,2.) -- (axis cs:0.,2.) -- (axis cs:0.,0.) -- (axis cs:9.,0.) -- (axis cs:9.,-0.5) -- (axis cs:-0.5,-0.5) -- cycle;
\fill[blue!40!white] (axis cs:4.5,0) rectangle (axis cs:5.5,1);
\draw [color=wqwqwq] (axis cs:-0.5,2.)-- (axis cs:0.,2.);
\draw [color=wqwqwq] (axis cs:0.,2.)-- (axis cs:0.,0.);
\draw [color=wqwqwq] (axis cs:0.,0.)-- (axis cs:9.,0.);
\draw [color=wqwqwq] (axis cs:9.,0.)-- (axis cs:9.,-0.5);
\draw [color=wqwqwq] (axis cs:9.,-0.5)-- (axis cs:-0.5,-0.5);
\draw [color=wqwqwq] (axis cs:-0.5,-0.5)-- (axis cs:-0.5,2.);
\draw [line width=1.6pt,color=aqaqaq,decorate,decoration={coil,segment length=6pt,amplitude=3mm}] (axis cs:0.,0.5)-- (axis cs:4.5,0.5);[![image not centering][1]][1]
\end{groupplot}
\end{tikzpicture}

\结束{文档}

答案1

整个混乱都出现了,因为你正在改变height“正确的情节”/图片的“ ”(通过改变height y)。因为这些图只是被group/horizontal sep和“分开” group/vertical sep(后者是这里感兴趣的一个),所以当只显示两行对时,您会看到奇怪的对齐方式,这种对齐方式可能看起来是随机的。但是,只需将对的第三行复制到groupplot问题代码中的环境中,您就可以清楚地看到这种模式。

因此,为了解决这个问题,你可以

  • 仅更改group/vertical sep右列的
  • height或者在两列中使用相同的内容。

在这里我展示了后一种情况,因为这在重复 1000 行时也会起作用,而在第一种情况下您必须vertical sep非常准确地设置才能仍然具有相同的对齐方式。

有关更多详细信息,请查看代码中的注释。

\documentclass[border=2pt]{standalone}
\usepackage{pgfplots}
    \usetikzlibrary{
        patterns,
        pgfplots.groupplots,
    }
    \pgfplotsset{
        % added `compat' level so coordinates can be written without "axis cs:"
        compat=1.11,
        %
        % moved the (same) axis options to styles
        my plot style/.style={
            width=7cm,
            height=6cm,
            xlabel=$x$,
            ylabel=$\sin x$,
            xtick={
                -6.28318, -4.7123889, -3.14159, -1.5708,
                1.5708, 3.14159, 4.7123889, 6.28318,7.853981,9.424777
            },
            xticklabels={
                $-2\pi$, $-\frac{3\pi}{2}$, $-\pi$, $-\frac{\pi}{2}$,
                $\frac{\pi}{2}$, $\pi$, $\frac{3\pi}{2}$, $2\pi$, $\frac{5\pi}{2}$, $3\pi$
            },
            typeset ticklabels with strut,      % <-- added
        },
        my pic style/.style={
            axis lines=none,
%        % --------------------------------------------------------------------
%        % these two lines cause the mess, because they change the height of
%        % of the plot which destroys the alignment
%            height=4cm,
%            y=0.75cm,
%        % (this not really, but because I think you should avoid using them
%        %  I also show another way to "rescale" the width)
%            x=0.75cm,
%        % --------------------------------------------------------------------
            % when not applying `x' any more
            % either you change `xmin' and `xmax' to get the "original" size
            % back or change the `width' of the plot (see below)
            xmin=-0.5,
            xmax=9,
%            % these have to be adapted (see below)
%            ymin=-1,
%            ymax=2,
        % ====================================================================
        % here are the adapted values to make the pics (almost) look the same
        % as before, but this time they are aligned "properly"/as you wanted
            % without using `x' the "`width'" of the plot has to be enlarged
            % (default value = `\axisdefaultwidth' = 240pt)
            width=290pt,
            % set the height to the same value as for the "left side"/the plots
            height=6cm,
            % to now "rescale" the height of the graph, change the Delta of
            % `ymax' and `ymin' until the height of the drawn pic is the same
            % as before. By then adjusting both values by keeping the same Delta
            % you can move the pic so that this fits your needs
            ymin=-2,
            ymax=3,
%%        % ---------------------------------------------------------------------
%%        % or use the original values and just change the `vertical sep' for
%%        % the right column/pics
%%        % (but if you want an \textbf{exact} alignment -- also after 1000 rows
%%        %  -- it will be hard work to find the exact sep via trial and error.
%%        %  Then it would be better to try to calculate the exact vertical sep
%%        %  needed by having a look at the manual who the coordinates are
%%        %  transformed)
%            /pgfplots/group/vertical sep=28mm,
%%        % ====================================================================
        },
    }
    \definecolor{aqaqaq}{rgb}{0.6274509803921569,0.6274509803921569,0.6274509803921569}
    \definecolor{wqwqwq}{rgb}{0.3764705882352941,0.3764705882352941,0.3764705882352941}

    \newcommand*\MyPlot{
        \addplot [smooth,blue,mark=none,domain=0:2*pi,samples=40]{cos(deg(x))};
%    % because of the "new" `compat' level you need to adjust the coordinates
%    % of the rectangle now
%    % (because in the original they are almost not noticeable, I commented it
%    %  here)
%        \draw[red,dashed,thick] (3,10) rectangle (5,25);
    }
    \newcommand*\MyPic{
%        \addplot coordinates{(0,1)};       % <-- not needed
        \draw [->,color=black] (-0.5,0.) -- (9.5,0.)
            node [at end,right] {$x$};
        \fill [color=wqwqwq,fill=wqwqwq,pattern=north east lines,pattern color=wqwqwq]
            (-0.5,2.) -- (0.,2.) -- (0.,0.) -- (9.,0.)
                -- (9.,-0.5) -- (-0.5,-0.5) -- cycle;
        \fill [blue!40!white] (4.5,0) rectangle (5.5,1);
        \draw [color=wqwqwq]  (-0.5,2.)-- (0.,2.);
        \draw [color=wqwqwq]  (0.,2.)-- (0.,0.);
        \draw [color=wqwqwq]  (0.,0.)-- (9.,0.);
        \draw [color=wqwqwq]  (9.,0.)-- (9.,-0.5);
        \draw [color=wqwqwq]  (9.,-0.5)-- (-0.5,-0.5);
        \draw [color=wqwqwq]  (-0.5,-0.5)-- (-0.5,2.);
        \draw [line width=1.6pt,color=aqaqaq,decorate,
            decoration={coil,segment length=6pt,amplitude=3mm}]
                (0.,0.5)-- (4.5,0.5);
    }
\begin{document}
    \begin{tikzpicture}
        \begin{groupplot}[
            group style={group size=2 by 3},
            no markers,
            axis y line=center,
            axis x line=middle,
            enlargelimits=true,
            every axis x label/.style={
                at={(ticklabel* cs:1)},
                anchor=west,
            },
            every axis y label/.style={
                at={(ticklabel* cs:1)},
                anchor=south,
            },
        ]
        % -------------------------------------------
%        % use the pic as first plot instead of the
%        % plot with original settings to check for
%        % the right height
%        \nextgroupplot[
%            xmin=-0.5,
%            xmax=9,
%            ymin=-1,
%            ymax=2,
%            height=4cm,
%            x=0.75cm,
%            y=0.75cm,
%        ]
%            \MyPic
        % ----------
        \nextgroupplot[my plot style]
            \MyPlot
        % -------------------------------------------
        \nextgroupplot[my pic style]
            \MyPic
        \nextgroupplot[my plot style]
            \MyPlot
        \nextgroupplot[my pic style]
            \MyPic
        \nextgroupplot[my plot style]
            \MyPlot
        \nextgroupplot[my pic style]
            \MyPic
        \end{groupplot}
    \end{tikzpicture}
\end{document}

该图显示了上述代码的结果

相关内容