如何在Overleaf/LaTeX中创建分组图(Groupplot)?
将单折线图拆分为3×3子图布局的实现方法(Overleaf + PGFPlots)
问题描述
我正在使用Overleaf制作图表,目前完成了一张包含9条曲线的折线图,但希望将每条曲线单独拆分为子图,并以3行3列的形式排列布局。以下是当前使用的代码:
\documentclass[12pt]{article} \usepackage{pgfplots} \usepackage{pgfplotstable} \usepackage{tikz} \begin{document} \begin{figure}[!htb] \centering \caption{Trait Attacks in the Swedish Multi-Party System 1970-2022.} \begin{tikzpicture} \pgfplotsset{scale only axis,} \begin{axis}[x tick label style={/pgf/number format/.cd,% scaled x ticks = false, set thousands separator={}, fixed}, yticklabel={$\pgfmathprintnumber{\tick}\%$}, grid=both, minor tick num=9, grid style={line width=.2pt, draw=black!10}, major grid style={line width=.4pt, draw=black!20}, enlargelimits={abs=0.1}, width=12cm, height=8cm, xmin=1970, xmax=2022, ymin=0, ymax=30, xlabel= \textbf{Time}, ylabel= \textbf{Degree of Trait Attacks}, xtick {1970,1973,1976,1979,1982,1985,1988,1991,1994,1998,2002,2006,2010,2014,2018,2022}, every tick label/.append style={font=\tiny}] \addplot[color=purple,,mark=*] coordinates{ (1970,9.803922) (1973,3.703704) (1976,3.738318) (1979,3.726708) (1982,5.882353) (1985,6.4516129) (1988,1.5463918) (1991,2.419355) (1994,3.7593985) (1998,1.538462) (2002,2.272727) (2006,3.703704) (2010,0) (2014,0) (2018,0) (2022,0)}; \addplot[color=red,,mark=*] coordinates{ (1970,9.489051) (1973,3.703704) (1976,6.666667) (1979,9.183673) (1982,25) (1985,6.849315) (1988,3.703704) (1991,4.4247788) (1994,3.149606) (1998,3.703704) (2002,3.703704) (2006,3.703704) (2010,0) (2014,0) (2018,0) (2022,0)}; \addplot[color=lime,,mark=*] coordinates{ (1988,3.703704) (1991,0) (1994,3.703704) (1998,1.234568) (2002,3.703704) (2006,3.703704) (2010,0) (2014,0) (2018,0) (2022,0)}; \addplot[color=green,,mark=*] coordinates{ (1970,3.703704) (1973,3.703704) (1976,1.923077) (1979,3.2942104) (1982,3.703704) (1985,3.703704) (1988,3.703704) (1991,3.448276) (1994,3.703704) (1998,3.703704) (2002,4.5112782) (2006,3.703704) (2010,0) (2014,0) (2018,0) (2022,0)}; \addplot[color=blue,,mark=*] coordinates{ (1970,3.703704) (1973,3.703704) (1976,3.703704) (1979,3.703704) (1982,3.703704) (1985,4.8275862) (1988,3.703704) (1991,3.333333) (1994,5) (1998,4.242424) (2002,2.1097046) (2006,4.3010753) (2010,0) (2014,0) (2018,0) (2022,0)}; \addplot[color=teal,mark=*] coordinates{ (1991,3.703704) (1994,3.703704) (1998,1.020408) (2002,2.139037) (2006,1.8348624) (2010,0) (2014,0) (2018,0) (2022,0)}; \addplot[color=brown,,mark=*] coordinates{ (2010,0) (2014,0) (2018,0) (2022,0)}; \addplot[color=cyan,,mark=*] coordinates{ (1970,9.756098) (1973,2.564103) (1976,2) (1979,5.333333) (1982,6.25) (1985,6.593407) (1988,3.703704) (1991,0.621118) (1994,1.298701) (1998,3.703704) (2002,0.7272727) (2006,3.703704) (2010,0) (2014,0) (2018,0) (2022,0)}; \addplot[color=yellow,,mark=*] coordinates{ (1991,2.1390374) (1994,0)}; \legend{V, S, MP, C, L, KD, SD, M, NyD} \end{axis} \end{tikzpicture} \end{figure} \end{document}
实现方案
使用PGFPlots内置的groupplots库可以高效实现3×3的子图布局,无需额外依赖包。以下是修改后的完整代码及关键说明:
修改后代码
\documentclass[12pt]{article} \usepackage{pgfplots} \usepackage{pgfplotstable} \usepackage{tikz} \usepgfplotslibrary{groupplots} % 加载分组绘图库 \begin{document} \begin{figure}[!htb] \centering \caption{Trait Attacks in the Swedish Multi-Party System 1970-2022.} \begin{tikzpicture} % 全局轴样式,所有子图共用 \pgfplotsset{ scale only axis, x tick label style={/pgf/number format/.cd, scaled x ticks = false, set thousands separator={}, fixed}, yticklabel={$\pgfmathprintnumber{\tick}\%$}, grid=both, minor tick num=9, grid style={line width=.2pt, draw=black!10}, major grid style={line width=.4pt, draw=black!20}, enlargelimits={abs=0.1}, width=4cm, % 调整子图宽度适配3列 height=3cm, % 调整子图高度适配3行 xmin=1970, xmax=2022, ymin=0, ymax=30, xlabel=\textbf{Time}, ylabel=\textbf{Degree of Trait Attacks}, xtick={1970,1973,1976,1979,1982,1985,1988,1991,1994,1998,2002,2006,2010,2014,2018,2022}, every tick label/.append style={font=\tiny}, legend style={font=\tiny} } \begin{groupplot}[ group style={ group size=3 by 3, % 3行3列布局 horizontal sep=1cm, % 子图水平间距 vertical sep=1.2cm, % 子图垂直间距 ylabels at=edge left, % 仅左侧子图显示Y轴标签 xlabels at=edge bottom % 仅底部子图显示X轴标签 } ] % 子图1:V(紫色) \nextgroupplot \addplot[color=purple,mark=*] coordinates{ (1970,9.803922) (1973,3.703704) (1976,3.738318) (1979,3.726708) (1982,5.882353) (1985,6.4516129) (1988,1.5463918) (1991,2.419355) (1994,3.7593985) (1998,1.538462) (2002,2.272727) (2006,3.703704) (2010,0) (2014,0) (2018,0) (2022,0)}; \legend{V} % 子图2:S(红色) \nextgroupplot \addplot[color=red,mark=*] coordinates{ (1970,9.489051) (1973,3.703704) (1976,6.666667) (1979,9.183673) (1982,25) (1985,6.849315) (1988,3.703704) (1991,4.4247788) (1994,3.149606) (1998,3.703704) (2002,3.703704) (2006,3.703704) (2010,0) (2014,0) (2018,0) (2022,0)}; \legend{S} % 子图3:MP(石灰绿) \nextgroupplot \addplot[color=lime,mark=*] coordinates{ (1988,3.703704) (1991,0) (1994,3.703704) (1998,1.234568) (2002,3.703704) (2006,3.703704) (2010,0) (2014,0) (2018,0) (2022,0)}; \legend{MP} % 子图4:C(绿色) \nextgroupplot \addplot[color=green,mark=*] coordinates{ (1970,3.703704) (1973,3.703704) (1976,1.923077) (1979,3.2942104) (1982,3.703704) (1985,3.703704) (1988,3.703704) (1991,3.448276) (1994,3.703704) (1998,3.703704) (2002,4.5112782) (2006,3.703704) (2010,0) (2014,0) (2018,0) (2022,0)}; \legend{C} % 子图5:L(蓝色) \nextgroupplot \addplot[color=blue,mark=*] coordinates{ (1970,3.703704) (1973,3.703704) (1976,3.703704) (1979,3.703704) (1982,3.703704) (1985,4.8275862) (1988,3.703704) (1991,3.333333) (1994,5) (1998,4.242424) (2002,2.1097046) (2006,4.3010753) (2010,0) (2014,0) (2018,0) (2022,0)}; \legend{L} % 子图6:KD(蓝绿色) \nextgroupplot \addplot[color=teal,mark=*] coordinates{ (1991,3.703704) (1994,3.703704) (1998,1.0204
相关产品推荐
相关产品推荐

