1. 程式人生 > >【漫漫科研路\pgfplots】子圖的多種畫法

【漫漫科研路\pgfplots】子圖的多種畫法

在科研論文寫作中,有時候為了橫向、縱向對比或者節省空間,我們需要畫子圖,在MATLAB中可以通過subplot命令來實現。在Latex中有以下幾種方法進行子圖的繪製:
- 使用subfig巨集包(有可能與hyperref巨集包衝突,推薦使用subcaption巨集包),主要格式為:

  \begin{figure}
  \subfloat[]{}
  \subfloat[]{}\\
  \subfloat[]{}
  \subfloat[]{}
  \end{figure}
  • 使用subcaption巨集包,主要格式為:
  \begin{figure}
  \subcaptionbox{}
{} \subcaptionbox{}{}\\ \subcaptionbox{}{} \subcaptionbox{}{} \end{figure}
  • 使用groupplot巨集包,主要格式為:
  \begin{figure}
\centering
\begin{tikzpicture}
\begin{groupplot}
\nextgroupplot
\addplot {x};
\nextgroupplot
\end{groupplot}
\end{tikzpicture}
\end{figure}
  • 使用matrix巨集包,主要格式為:
    \begin{figure}
\centering \begin{tikzpicture} \matrix { \begin{axis} \addplot {x}; \end{axis} \begin{axis} \addplot {x}; \end{axis} } \end{tikzpicture} \end{figure}

下面給出上述各種情況的具體程式碼實現以及示意圖:
- 使用subfig巨集包
這裡寫圖片描述
程式碼如下:

\documentclass{article}
\usepackage[dvipdfm]{graphicx}
\def\pgfsysdriver{pgfsys-dvipdfmx.def}
\usepackage{tikz} \usepackage{pgfplots} \usepackage{subfig}%使用子圖包,可能與hyperref衝突 \usepackage{float} \usepackage{cite} \usepackage[colorlinks,dvipdfm, pdfpagemode=UseOutlines, pdfstartview=FitH, anchorcolor=green, citecolor=blue, linkcolor=red, hyperindex=true, pagebackref, bookmarksnumbered, bookmarksopenlevel=2, colorlinks]{hyperref} \pgfplotsset{width=6cm,compat=1.15} \begin{document} \begin{figure} \begin{center} \subfloat[\label{subfig1}]{ \begin{tikzpicture} \begin{axis}[ legend columns=-1,%the legend are plotted horizontally legend entries={$x$}, legend to name=named,% stored in named, not plotted in the figure title={subfig1}, ] \addplot {x};\label{curvex} \end{axis} \end{tikzpicture}\label{subfig2}} \subfloat[]{ \begin{tikzpicture} \begin{axis}[title={subfig2}] \addplot {x}; \end{axis} \end{tikzpicture}\label{subfig2}} \end{center} \caption{\hspace{1em}Two subfigures.}\label{f1} \end{figure} As depicted in Figures~\ref{subfig1} and \ref{subfig2}, the subfigures of Figure~\ref{f1}, \ref{curvex} represents function $f(x)=x$. \end{document}
  • 使用subcaption巨集包
    這裡寫圖片描述
    程式碼如下:
\documentclass{article}
\usepackage[dvipdfm]{graphicx}
\def\pgfsysdriver{pgfsys-dvipdfmx.def}
\usepackage{tikz}
\usepackage{pgfplots}
%\usepackage{subfig}
\usepackage[hypcap=true,labelsep=period,font=small]{caption}% 圖的標題設定Fig.
\usepackage[hypcap=true]{subcaption}%用於畫子圖 可以適配hyperref包
\usepackage{float}
\usepackage[colorlinks,dvipdfm,
            pdfpagemode=UseOutlines,
            pdfstartview=FitH,
            anchorcolor=green,
            citecolor=blue,
            linkcolor=red,
            hyperindex=true,
            pagebackref,
            bookmarksnumbered,
            bookmarksopenlevel=2,
            colorlinks]{hyperref}
\pgfplotsset{width=6cm,compat=1.15}
\begin{document}
\begin{figure}
\begin{center}
\subcaptionbox{\label{subfig1}}{
\begin{tikzpicture}
\begin{axis}[
legend columns=-1,%the legend are plotted horizontally
legend entries={$x$},
legend to name=named,% stored in named
title={subfig1},
]
\addplot {x};\label{curvex}
\end{axis}
\end{tikzpicture}}
\subcaptionbox{\label{subfig2}}{
\begin{tikzpicture}
\begin{axis}[title={subfig2}]
\addplot {x};
\end{axis}
\end{tikzpicture}}
\end{center}
\caption{\hspace{1em}Two subfigures.}\label{f1}
\end{figure}

As depicted in Figures~\ref{subfig1} and \ref{subfig2}, the subfigures of Figure~\ref{f1}, \ref{curvex} represents function $f(x)=x$.

\end{document} 
  • 使用groupplot巨集包
    這裡寫圖片描述
    程式碼如下:
\documentclass{article}
\usepackage[dvipdfm]{graphicx}
\def\pgfsysdriver{pgfsys-dvipdfmx.def}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{width=7cm,compat=1.15}
\usepgfplotslibrary{groupplots}
\usepackage{caption}
\usepackage{subcaption}
\usepackage[colorlinks,dvipdfm,
            pdfpagemode=UseOutlines,
            pdfstartview=FitH,
            anchorcolor=green,
            citecolor=blue,
            linkcolor=red,
            hyperindex=true,
            pagebackref,
            bookmarksnumbered,
            bookmarksopenlevel=2,
            colorlinks]{hyperref}
\begin{document}
\begin{figure}[htb]
\centering
\begin{tikzpicture}
\begin{groupplot}[
group style={
group name=my plots,
group size=2 by 2,
xlabels at=edge bottom,
xlabels at=all,
ylabels at=edge left,
x descriptions at=edge bottom,
},
footnotesize,
width=6cm,
height=6cm,
%
xlabel=$x$,
ylabel=$f(x)$,
]
\nextgroupplot
\addplot {x};
\node [text width=1em,anchor=north west] at (rel axis cs: 0,1)
                {\subcaption{\label{f11}}};%<- changed
\nextgroupplot
\addplot {x^2};
\nextgroupplot
\addplot {x^3};
\nextgroupplot
\addplot {x^4};
\end{groupplot}
\end{tikzpicture}
\caption{\hspace{1em}Four subfigures.}\label{f1}
\end{figure}
How to refer to subfigure~\ref{f11} in Figure~\ref{f1}.

\end{document} 
  • 使用matrix巨集包
    這裡寫圖片描述
    程式碼如下:
\documentclass{article}
\usepackage[dvipdfm]{graphicx}
\def\pgfsysdriver{pgfsys-dvipdfmx.def}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{width=7cm,compat=1.15}
\usepgfplotslibrary{groupplots}
\usetikzlibrary{matrix}
\usepackage{caption}
\usepackage{subcaption}
\usepackage[colorlinks,dvipdfm,
            pdfpagemode=UseOutlines,
            pdfstartview=FitH,
            anchorcolor=green,
            citecolor=blue,
            linkcolor=red,
            hyperindex=true,
            pagebackref,
            bookmarksnumbered,
            bookmarksopenlevel=2,
            colorlinks]{hyperref}
\begin{document}
\begin{figure}[htb]
\centering
\begin{tikzpicture}
\pgfplotsset{small}
\matrix {
\begin{axis}[ylabel={$f(x)=x$},ylabel style={font=\small}]
\addplot {x};
\node [text width=1em,anchor=north west] at (rel axis cs: 0,1)
                {\subcaption{\label{f11}}};%<- changed
\end{axis}
&
% differently large labels are aligned automatically:
\begin{axis}[ylabel={$f(x)=x^2$},ylabel style={font=\small}]
\addplot {x^2};
\end{axis}
\\
\begin{axis}[ylabel={$f(x)=x^3$},ylabel style={font=\small},xlabel=$x$,xlabel style={font=\small}]
\addplot {x^3};
\end{axis}
&
\begin{axis}[ylabel={$f(x)=x^4$},ylabel style={font=\small},xlabel=$x$,xlabel style={font=\small}]
\addplot {x^4};
\end{axis}
\\
};
\end{tikzpicture}
\caption{\hspace{1em}Four subfigures.}\label{f1}
\end{figure}
How to refer to subfigure~\ref{f11} in Figure~\ref{f1}.

\end{document} 

Note: 第三種和第四種方法不適合需要單獨引用每一個子圖的情況,比較適合把四張圖看成一個整體的情況。