调整 PGFplots 图的大小和位置

调整 PGFplots 图的大小和位置

我创建了一个 2x2 数组,其中每个框包含一个子图。对于三个子图,我使用\addplot3PGFplots 中的colorbar,另一个子图为\addplot。由于使用了colorbar,四个子图的对称性被破坏了。我想知道是否有办法重新缩放唯一图,使其与其他 3 个子图对齐colorbar

下面是我的代码示例:

\documentclass[a4paper,12pt]{article}

\usepackage{pgfplots}
\usepgfplotslibrary{patchplots,colormaps}

\usepackage{subfigure}
\usepackage[a4paper]{geometry}
\usepackage{array}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}

\begin{document}
\subsection{First subsection}
\begin{figure}[htbp]
\hspace{-0.5cm}
\begin{tabular}{C{.48\textwidth}C{.48\textwidth}}
 %%%%%%%%%%%%%%%%%%%%%% 1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 \subfigure [Subfigure 1] {
 \resizebox{0.5\textwidth}{!}{%
  \begin{tikzpicture} %
  \begin{axis}[view={0}{90}, %
               xlabel={$p$}, %
           ylabel={Mean}, %
               title={Mean vs $p$}, %
               y filter/.code=\pgfmathparse{\pgfmathresult/400}, %
               colormap/autumn, %
               colorbar, %
               xmin=0, %
               xmax=0.1, %
       ymin=0, %
       ymax=1, %
              xticklabel style={/pgf/number format/.cd,fixed,precision=2}, %
              ] %
  \end{axis} %
 \end{tikzpicture}
}} & 
%%%%%%%%%%%%%%%%%%%%%% 2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subfigure [Subfigure 2] {
 \resizebox{0.5\textwidth}{!}{%
 \begin{tikzpicture} %
  \begin{axis}[view={0}{90}, %
               xlabel={$p$}, %
           ylabel={Mean}, %
               title={Mean vs $p$}, %
               colormap/cool,
       colorbar,
               xmin=0,%
               xmax=0.1, %
       ymin=0, %
       ymax=1, %
               xticklabel style={/pgf/number format/.cd,fixed,precision=2}, %
              ] %
  \end{axis} %
 \end{tikzpicture}
}} \\
%%%%%%%%%%%%%%%%%%%%%% 3 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 \subfigure [Subfigure 3] {
 \resizebox{0.5\textwidth}{!}{%
\begin{tikzpicture} %
  \begin{axis}[tiny,
       xlabel={$p$}, %
           ylabel={Mean}, %
               title={Mean vs $p$}, %
       height=5.2cm, width=6cm,% improve scaling manually
               xmin=0,%
               xmax=0.1, %
       ymin=0, %
       ymax=1, %
       only marks, %
       mark size=0.1pt, %
               xticklabel style={/pgf/number format/.cd,fixed,precision=2}, 
      ]
   \end{axis}
  \end{tikzpicture}
 }} & 
%%%%%%%%%%%%%%%%%%%%%% 4 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subfigure [Subfigure 4] {
 \resizebox{0.5\textwidth}{!}{%
 \begin{tikzpicture} %
  \begin{axis}[view={0}{90}, %
               xlabel={$p$}, %
           ylabel={Mean}, %
               title={Mean vs $p$}, %
               colormap/cool, %
       colorbar, %                   
       xmin=0, %
               xmax=0.1, %
       ymin=0, %
              ] %
  \end{axis} %
 \end{tikzpicture}
}} \\
\end{tabular}
\caption{Several options}
\end{figure}
\end{document}

结果如下:

将第三个 pgfplot 放高一点也许会更简单:

答案1

我不建议使用它\resizebox来获取所需的绘图大小,因为这也会缩放文本:同一页面上任意不同的字体大小看起来真的很不专业。

更精确地控制绘图大小的一种方法是使用键scale only axis,它指定widthheight命令仅适用于不带刻度标签、颜色条和标题的轴区域。然后,您可以将 设置width为某个适当的值:如果您希望每个绘图填满页面宽度的一半,则必须使用小于 的值0.5\textwidth,因为仍然需要容纳颜色条和标签。为了使绘图很好地对齐,您可以将选项传递trim axis left, trim axis righttikzpicture环境。这指定仅使用实际绘图区域来定位绘图。


如果您想在图表下方使用水平颜色条,您可以将选项添加baselinetikzpicture环境中。这将使图表的下边缘与文本基线对齐。


与垂直彩条对齐的代码:

\documentclass[a4paper,12pt]{article}

\usepackage{pgfplots}
\usepgfplotslibrary{patchplots,colormaps}

\usepackage{subfigure}
\usepackage[a4paper]{geometry}
\usepackage{array}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}
\pgfplotsset{
    every axis/.append style={
        scale only axis,
        width=0.25\textwidth,
        xtick={0,0.05,0.1},
    },
    /tikz/every picture/.append style={
        trim axis left,
        trim axis right,
    }
}
\begin{document}
\subsection{First subsection}
\begin{figure}[htbp]
\hspace{-0.5cm}
\begin{tabular}{C{.48\textwidth}C{.48\textwidth}}
 %%%%%%%%%%%%%%%%%%%%%% 1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 \subfigure [Subfigure 1] {
  \begin{tikzpicture} %
  \begin{axis}[
    view={0}{90}, %
    xlabel={$p$}, %
    ylabel={Mean}, %
    title={Mean vs $p$}, %
    y filter/.code=\pgfmathparse{\pgfmathresult/400}, %
    colormap/autumn, %
    colorbar, %
    xmin=0, %
    xmax=0.1, %
    ymin=0, %
    ymax=1, %
    xticklabel style={/pgf/number format/.cd,fixed,precision=2},
] %
  \end{axis} %
 \end{tikzpicture}
} & 
%%%%%%%%%%%%%%%%%%%%%% 2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subfigure [Subfigure 2] {
 \begin{tikzpicture} %
  \begin{axis}[view={0}{90}, %
               xlabel={$p$}, %
           ylabel={Mean}, %
               title={Mean vs $p$}, %
               colormap/cool,
       colorbar, 
               xmin=0,%
               xmax=0.1, %
       ymin=0, %
       ymax=1, %
               xticklabel style={/pgf/number format/.cd,fixed,precision=2}, %
              ] %
  \end{axis} %
 \end{tikzpicture}
} \\
%%%%%%%%%%%%%%%%%%%%%% 3 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 \subfigure [Subfigure 3] {

\begin{tikzpicture} %
  \begin{axis}[
       xlabel={$p$}, %
           ylabel={Mean}, %
               title={Mean vs $p$}, %
               xmin=0,%
               xmax=0.1, %
       ymin=0, %
       ymax=1, %
       only marks, %
       mark size=0.1pt, %
               xticklabel style={/pgf/number format/.cd,fixed,precision=2},
      ]
   \end{axis}
  \end{tikzpicture}
 } & 
%%%%%%%%%%%%%%%%%%%%%% 4 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subfigure [Subfigure 4] {
 \begin{tikzpicture} %
  \begin{axis}[view={0}{90}, %
               xlabel={$p$}, %
           ylabel={Mean}, %
               title={Mean vs $p$}, %
               colormap/cool, %
       colorbar, %                   
       xmin=0, %
               xmax=0.1, %
       ymin=0,ymax=1, %
       xticklabel style={/pgf/number format/.cd,fixed,precision=2},
              ] %
  \end{axis} %
 \end{tikzpicture}
} \\
\end{tabular}
\caption{Several options}
\end{figure}
\end{document}

与水平彩条对齐的代码:

\documentclass[a4paper,12pt]{article}

\usepackage{pgfplots}
\usepgfplotslibrary{patchplots,colormaps}

\usepackage{subfigure}
\usepackage[a4paper]{geometry}
\usepackage{array}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}
\pgfplotsset{
    every axis/.append style={
        scale only axis,
        width=0.3\textwidth,height=0.23\textwidth,
        xtick={0,0.05,0.1},
    },
    /tikz/every picture/.append style={
        trim axis left,
        trim axis right,
        baseline
    }
}
\begin{document}
\subsection{First subsection}
\begin{figure}[htbp]
\hspace{-0.5cm}
\begin{tabular}{C{.48\textwidth}C{.48\textwidth}}
 %%%%%%%%%%%%%%%%%%%%%% 1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 \subfigure [Subfigure 1] {
  \begin{tikzpicture} %
  \begin{axis}[
    view={0}{90}, %
    xlabel={$p$}, %
    ylabel={Mean}, %
    title={Mean vs $p$}, %
    y filter/.code=\pgfmathparse{\pgfmathresult/400}, %
    colormap/autumn, %
    colorbar horizontal, %
    xmin=0, %
    xmax=0.1, %
    ymin=0, %
    ymax=1, %
    xticklabel style={/pgf/number format/.cd,fixed,precision=2},
] %
  \end{axis} %
 \end{tikzpicture}
} & 
%%%%%%%%%%%%%%%%%%%%%% 2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subfigure [Subfigure 2] {
 \begin{tikzpicture} %
  \begin{axis}[view={0}{90}, %
               xlabel={$p$}, %
           ylabel={Mean}, %
               title={Mean vs $p$}, %
               colormap/cool,
       colorbar horizontal, 
               xmin=0,%
               xmax=0.1, %
       ymin=0, %
       ymax=1, %
               xticklabel style={/pgf/number format/.cd,fixed,precision=2}, %
              ] %
  \end{axis} %
 \end{tikzpicture}
} \\
%%%%%%%%%%%%%%%%%%%%%% 3 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 \subfigure [Subfigure 3] {

\begin{tikzpicture} %
  \begin{axis}[
       xlabel={$p$}, %
           ylabel={Mean}, %
               title={Mean vs $p$}, %
               xmin=0,%
               xmax=0.1, %
       ymin=0, %
       ymax=1, %
       only marks, %
       mark size=0.1pt, %
               xticklabel style={/pgf/number format/.cd,fixed,precision=2},
      ]
   \end{axis}
  \end{tikzpicture}
 } & 
%%%%%%%%%%%%%%%%%%%%%% 4 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subfigure [Subfigure 4] {
 \begin{tikzpicture} %
  \begin{axis}[view={0}{90}, %
               xlabel={$p$}, %
           ylabel={Mean}, %
               title={Mean vs $p$}, %
               colormap/cool, %
       colorbar horizontal, %                   
       xmin=0, %
               xmax=0.1, %
       ymin=0,ymax=1, %
       xticklabel style={/pgf/number format/.cd,fixed,precision=2},
              ] %
  \end{axis} %
 \end{tikzpicture}
} \\
\end{tabular}
\caption{Several options}
\end{figure}
\end{document}

相关内容