我想使用 \subcaption 库并排绘制两个图表。但是,我无法调整浮点数的宽度,这导致它们无法并排绘制。
\begin{filecontents*}{data.csv}
a,b,c,d
1,4,5,1
2,3,1,5
3,5,6,1
4,1,4,9
5,3,4,7
\end{filecontents*}
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{csvsimple}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{subcaption}
\begin{document}
\begin{figure}[h]
\centering
\begin{subfigure}[h]{0.2\textwidth}
\centering
\begin{tikzpicture}
\begin{axis}[
xlabel=$\mathcal{L}_1$,
ylabel=$\mathcal{L}_2$]
\addplot[scatter, only marks] table[ x index={0},y index={1}, col sep=comma]{data.csv};
\end{axis}
\end{tikzpicture}
\end{subfigure}
\hfill
\begin{subfigure}[h]{0.2\textwidth}
\centering
\begin{tikzpicture}
\begin{axis}[
xlabel=$\mathcal{L}_1$,
ylabel=$R_g$]
\addplot[scatter, only marks] table[x index={0},y index={1}, col sep=comma]{data.csv};
\end{axis}
\end{tikzpicture}
\end{subfigure}
\end{figure}
\end{document}
我确信这是一个简单的修复,但我对 LaTeX 还很陌生,仍在学习。我非常感谢您的帮助。
答案1
您需要调整图的宽度。我建议将子图加宽。
\begin{filecontents*}{data.csv}
a,b,c,d
1,4,5,1
2,3,1,5
3,5,6,1
4,1,4,9
5,3,4,7
\end{filecontents*}
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{csvsimple}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{subcaption}
\begin{document}
\begin{figure}[h]
\centering
\begin{subfigure}[h]{0.49\textwidth}
\centering
\begin{tikzpicture}
\begin{axis}[width=0.98\textwidth,
xlabel=$\mathcal{L}_1$,
ylabel=$\mathcal{L}_2$]
\addplot[scatter, only marks] table[ x index={0},y index={1}, col sep=comma]{data.csv};
\end{axis}
\end{tikzpicture}
\end{subfigure}
\hfill
\begin{subfigure}[h]{0.49\textwidth}
\centering
\begin{tikzpicture}
\begin{axis}[width=0.98\textwidth,
xlabel=$\mathcal{L}_1$,
ylabel=$R_g$]
\addplot[scatter, only marks] table[x index={0},y index={1}, col sep=comma]{data.csv};
\end{axis}
\end{tikzpicture}
\end{subfigure}
\end{figure}
\end{document}
或者清理版本(几乎所有内容都来自下面 Zarko 的评论,谢谢!!)
\begin{filecontents*}{data.csv}
a,b,c,d
1,4,5,1
2,3,1,5
3,5,6,1
4,1,4,9
5,3,4,7
\end{filecontents*}
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usepackage{subcaption}
\begin{document}
\begin{figure}[h]
\centering\pgfplotsset{width=\linewidth, xlabel=$\mathcal{L}_1$}
\begin{subfigure}[h]{0.49\textwidth}
\centering
\begin{tikzpicture}
\begin{axis}[ylabel=$\mathcal{L}_2$]
\addplot[scatter, only marks] table[ x index={0},y index={1}, col sep=comma]{data.csv};
\end{axis}
\end{tikzpicture}
\end{subfigure}
\hfill
\begin{subfigure}[h]{0.49\textwidth}
\centering
\begin{tikzpicture}
\begin{axis}[ylabel=$R_g$]
\addplot[scatter, only marks] table[x index={0},y index={1}, col sep=comma]{data.csv};
\end{axis}
\end{tikzpicture}
\end{subfigure}
\end{figure}
\end{document}