[我正在尝试使用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}
您的代码有两个问题。
[图形的第一次水平对齐是错误的,但是可以使用]轻松地将其固定>
\begin{subfigure}[b]{0.3\textwidth}
代替
,不进行重新缩放。\begin{subfigure}[b!]{0.3\textwidth}
关于宽度,创建子图形环境时要做的是创建指定宽度的最小页面。 但是您可以根据自己的内容来尊重此宽度
例如,如果在子图中包含图像并为其提供\ linewidth的宽度,则将遵守该宽度。但是,如果将此图像的宽度设置为15厘米,则可能会比您的小图更大。但是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.
在轴环境中添加宽度参数
使用adjustbox重新缩放
顺便说一句,如果您不打算在绘图中添加子标题,则子图环境将无用,您可以将(适当缩放的)tikz图片并排并用\ hfill隔开。