我正在尝试使用https://www.latex-tutorial.com/tutorials/figures/中显示的 subfigure 方法来制作并排图,但我似乎无法调整大小并使它们并排...我究竟做错了什么?下面是我正在使用的代码
\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}
Run Code Online (Sandbox Code Playgroud)
您的代码有两个问题。
图形的第一次水平对齐不正确,但这可以通过使用轻松修复
\begin{subfigure}[b]{0.3\textwidth}
Run Code Online (Sandbox Code Playgroud)
代替
\begin{subfigure}[b!]{0.3\textwidth}
Run Code Online (Sandbox Code Playgroud)
关于宽度,创建子图环境时所做的是创建指定宽度的小型页面。但这取决于您是否尊重内容的宽度,不进行重新缩放。
例如,如果在子图中包含图像并为其指定宽度 \linewidth,则将尊重该宽度。但如果你给这张图片设置 15 厘米的宽度,它可能会比你的迷你页大。但 LaTeX 会尊重您的指令(并指示一个过满的水平盒)。
这就是你遇到的问题。你的地块太大并且重叠。
有两种方法可以解决这个问题。
您可以为轴环境提供 width=\linewidth 参数,但通常需要重新设计绘图
您可以重新缩放 tikz 创建的框。最灵活的方法是使用 adjustmentbox 包
\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.
Run Code Online (Sandbox Code Playgroud)
向轴环境添加宽度参数
使用调整框重新缩放
顺便说一句,如果您不打算在绘图中添加子标题,则子图环境是无用的,您可以将(正确缩放的)tikz图片并排放置,并用 \hfill 分隔。
归档时间: |
|
查看次数: |
32056 次 |
最近记录: |