首页 > 解决方案 > 如何在乳胶中对齐两个tikzpicture图

问题描述

我正在尝试使用https://www.latex-tutorial.com/tutorials/figures/中显示的子图方法来制作并排图,但我似乎无法调整大小并让它们并排...我究竟做错了什么?下面是我正在使用的代码

\begin{figure}
        \centering
        \begin{subfigure}[b!]{0.3\textwidth}
            \begin{tikzpicture}
                \begin{axis}[
                    axis y line = middle,
                    axis x line = middle,
                    xlabel = $x$,
                    ylabel = {$f(x) = x^3$},
                    grid=major,
                ]
                \addplot [
                    domain=-3:3, 
                    samples=100, 
                    color=red,
                ]
                {x^3};
                \addlegendentry{$x^3$}
                %
                \addplot [
                    domain=-3:3, 
                    samples=100, 
                    color=blue,
                    ]
                    {x^3 + 3};
                \addlegendentry{$x^3 + 3$}
                 %
                \addplot [
                    domain=-3:3, 
                    samples=100, 
                    color=green,
                    ]
                    {x^3 - 3};
                \addlegendentry{$x^3 - 3$}
                \end{axis}
            \end{tikzpicture}
        \end{subfigure}
        %\hfill
        \begin{subfigure}[b]{0.3\textwidth}
            \begin{tikzpicture}
                \begin{axis}[
                    axis y line = middle,
                    axis x line = middle,
                    xlabel = $x$,
                    ylabel = {$f(x) = x^3$},
                    grid=major,
                ]
                \addplot [
                    domain=-3:3, 
                    samples=100, 
                    color=red,
                ]
                {x^3};
                \addlegendentry{$x^3$}
                \end{axis}
            \end{tikzpicture}
        \end{subfigure}
        \caption{lajsdfls}
    \end{figure}

标签: latextikz

解决方案


您的代码有两个问题。

图形的第一次水平对齐不正确,但这可以通过使用轻松修复

\begin{subfigure}[b]{0.3\textwidth}

代替

\begin{subfigure}[b!]{0.3\textwidth}

关于宽度,当您创建子图环境时所做的是创建指定宽度的微型页面。但是,您的内容是否尊重此宽度取决于您,不进行重新缩放。

例如,如果在一个子图中,你包含一个图像并给它一个宽度\linewidth,那么宽度将被尊重。但是如果你给这个图像一个 15 厘米的宽度,它可能会比你的 minipage 大。但是 LaTeX 会尊重您的指令(并表示 hbox 过满)。

这是您遇到的问题。您的地块太大并且重叠。

有两种方法可以解决这个问题。

  • 您可以为轴环境提供 width=\linewidth 参数,但通常需要重新设计绘图

  • 您可以重新缩放 tikz 创建的框。最灵活的方法是使用 adjustbox 包

\documentclass{article}

\usepackage{subcaption}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{adjustbox}

\begin{document}

\begin{figure}
  \centering
  \begin{subfigure}[b]{0.45\textwidth}
%%%    \begin{adjustbox}{width=\linewidth} % rescale box
    \begin{tikzpicture}
      \begin{axis}[
%%%        width=\linewidth,            % or modify the plot width
        axis y line = middle,
        ...
        ...
      \end{axis}
    \end{tikzpicture}
%%%  \end{adjustbox}       %
\end{subfigure}%
   \hfill
  \begin{subfigure}[b]{0.45\textwidth}
etc.

向轴环境添加宽度参数

在此处输入图像描述

使用调整框重新缩放

在此处输入图像描述

顺便说一句,如果您不打算在绘图中添加子标题,则 subfigure 环境是无用的,您可以将(适当缩放的)tikzpictures 并排放置,并用 \hfill 分隔。


推荐阅读