PGFPlots:2x2 网格中的四个不同图形

PGFPlots:2x2 网格中的四个不同图形

考虑以下代码

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\pgfplotsset{every axis legend/.append style={at={(axis cs:1,-1)},draw=none,anchor=north west}}

\begin{document}
\begin{tikzpicture}
  \begin{axis}[
      axis lines=middle,
      domain=-2:2,
      restrict y to domain=-2:2,
      ticks=none,
      xlabel={$x$},
      ylabel={$y$},
      samples=100,
    ]
    \addplot[red, ultra thick](x,{x});
    \addplot[blue, ultra thick](x,{x^2});
    \addplot[green, ultra thick](x,{cos(deg(x))});
    \addplot[yellow, ultra thick](x,{sin(deg(x))});
    \legend{$a$,$b$,$c$,$d$}
\end{axis}
\end{tikzpicture}             
\end{document}

生成如下图片:

四张图

我想要将四个图表放在 2x2 的网格中,如下所示:

在此处输入图片描述

答案1

使用groupplots图书馆

\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary{calc}
\usepgfplotslibrary{groupplots}
\pgfplotsset{compat=1.16}
\pgfplotsset{every axis legend/.append style={at={(axis cs:1,-1)},draw=none,anchor=north west}}

\begin{document}
\begin{tikzpicture}
  \begin{groupplot}[
      axis lines=middle,
      domain=-2:2,
      restrict y to domain=-2:2,
      ymin=-2, ymax=2,
      ticks=none,
      xlabel={$x$},
      ylabel={$y$},
      samples=100,
      group style={group size=2 by 2},
      width=5cm,
      height=5cm,
      ]
    \nextgroupplot
    \addplot[red, ultra thick](x,{x});
    \addlegendentry{$a$}
    \nextgroupplot
    \addplot[blue, ultra thick](x,{x^2});
    \addlegendentry{$b$}
    \nextgroupplot
    \addplot[green, ultra thick](x,{cos(deg(x))});
    \addlegendentry{$c$}
    \nextgroupplot
    \addplot[yellow, ultra thick](x,{sin(deg(x))});
    \addlegendentry{$d$}
  \end{groupplot}
  \draw
    ($(group c1r1.north east)!.5!(group c2r1.north west)$) --
    ($(group c1r2.south east)!.5!(group c2r2.south west)$);
  \draw
    ($(group c1r1.south west)!.5!(group c1r2.north west)$) --
    ($(group c2r1.south east)!.5!(group c2r2.north east)$);
\end{tikzpicture}             
\end{document}

在此处输入图片描述

相关内容