将 pgfplot 图形并排放置在两列格式中

将 pgfplot 图形并排放置在两列格式中

我正在尝试将三张图表以两列格式并排放置。图表如下所示。

我的 LaTeX 代码如下:

   \begin{figure*}
   \centering
   %\includegraphics[height=4.5in]{state3}
   \label{fig:graph1}
   \begin{tikzpicture}[yscale=0.7, xscale=0.7]
   \begin{axis}[
   xmax=100,xmin=0,
   ymin= 0,ymax=1,
   xlabel=\emph{User Requests},ylabel=\emph{$E_{res}$ Percent},
   xtick={0,10,20,...,100},
   ytick={0,0.1,0.2,...,1},
   legend style={legend pos=north east}
   ]

   \addplot coordinates{(0,1.0) (10,0.96) (20,0.90) (30,0.85) (40,0.81)          
   (50,0.74) (60,0.70) (70,0.64) (80,0.59) (90,0.53) (100,0.47)};
   \addplot coordinates{(0,1.0) (10,0.93) (20,0.85) (30,0.78) (40,0.71)    
    (50,0.65) (60,0.57) (70,0.50) (80,0.43) (90,0.36) (100,0.29)};
    \addplot coordinates{(0,1.0) (10,0.92) (20,0.82) (30,0.70) (40,0.62)  
    (50,0.53) (60,0.43) (70,0.35) (80,0.26) (90,0.18) (100,0.11)};
    \legend{\emph{10 Nodes},\emph{8 Nodes},\emph{6 Nodes}}
    \end{axis}
    \end{tikzpicture}
     %\caption{$E_{res}$ vs Total User Requests for Requests Demanding 2-4  
     $vCPU$, 0-2 GB $Memory$}
     %\centering
     %\includegraphics[height=4.5in]{state3}
    \label{fig:graph2}
    \begin{tikzpicture}[yscale=0.7, xscale=0.7]
    \begin{axis}[
    xmax=100,xmin=0, 
    ymin= 0,ymax=1,
    xlabel=\emph{User Requests},ylabel=\emph{$E_{res}$ Percent},
    xtick={0,10,20,...,100},
    ytick={0,0.1,0.2,...,1},
    legend style={legend pos=north east}
    ]

    \addplot coordinates{(0,1.0) (10,0.94) (20,0.88) (30,0.82) (40,0.75)  
    (50,0.68) (60,0.62) (70,0.55) (80,0.49) (90,0.42) (100,0.34)};
    \addplot coordinates{(0,1.0) (10,0.92) (20,0.83) (30,0.74) (40,0.67)         
    (50,0.58) (60,0.50) (70,0.43) (80,0.35) (90,0.27) (100,0.19)};
    \addplot coordinates{(0,1.0) (10,0.90) (20,0.81) (30,0.70) (40,0.60)  
    (50,0.51) (60,0.42) (70,0.32) (80,0.23) (90,0.11) (100,0.04)};
    \legend{\emph{10 Nodes},\emph{8 Nodes},\emph{6 Nodes}}
    \end{axis}
    \end{tikzpicture}
    %\caption{$E_{res}$ vs Total User Requests for Requests Demanding 3-5  
    $vCPU$, 2-4 GB $Memory$}

     %\centering  
     %\includegraphics[height=4.5in]{state3}
     \label{fig:graph3}
     \begin{tikzpicture}[yscale=0.7, xscale=0.7] 
     \begin{axis}[
     xmax=100,xmin=0,
     ymin= 0,ymax=1,
     xlabel=\emph{User Requests},ylabel=\emph{$E_{res}$ Percent},
     xtick={0,10,20,...,100},
     ytick={0,0.1,0.2,...,1},
     legend style={legend pos=north east}
     ]

     \addplot coordinates{(0,1.0) (10,0.93) (20,0.86) (30,0.81) (40,0.77)  
     (50,0.69) (60,0.60) (70,0.53) (80,0.45) (90,0.38) (100,0.32)};
     \addplot coordinates{(0,1.0) (10,0.91) (20,0.84) (30,0.77) (40,0.70) 
     (50,0.63) (60,0.53) (70,0.44) (80,0.35) (90,0.26) (100,0.18)};
     \addplot coordinates{(0,1.0) (10,0.89) (20,0.80) (30,0.72) (40,0.63) 
     (50,0.53) (60,0.46) (70,0.37) (80,0.28) (90,0.18) (100,0.11)};
     \legend{\emph{10 Nodes},\emph{8 Nodes},\emph{6 Nodes}}
     \end{axis} 
     \end{tikzpicture}
     %\caption{$E_{res}$ Percent vs Total User Requests for Requests   
     Demanding 2-4 $vCPU$, 2-4 GB $Memory$}
     \end{figure*}

在此处输入图片描述

我的问题: 图表并非设置在一行中。

答案1

该问题本质上与@percusse 在其评论中指出的情况无关pgfplots:图形太大,无法放入水平空间,因此 TeX 会断线。

我发现你的问题其实不止是它的标题:你 \caption的图中有三个 s,但figure只支持一个。这表明我的答案可能不适合你的用例,你可能需要研究如何在一个图中添加多个标题以及如何对齐子图(有专门用于子图的包,只是我现在手头没有它们的名字)。

尽管如此,告诉 TeX不是断线是在元素之间使用不间断的空格。

在讨论我的修改之前,让我先展示一下结果:

\documentclass[10pt]{amsart}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}

\begin{document}
   \centering
   \pgfplotsset{
    small,
    %legend style={legend pos=north east}
    legend style={
        at={(0.01,0.01)},
        anchor=south west,
    },
   }%
   \begin{tikzpicture}[baseline]
   \begin{axis}[
   xmax=100,xmin=0,
   ymin= 0,ymax=1,
   xlabel=\emph{User Requests},ylabel=\emph{$E_{res}$ Percent},
   xtick={0,10,20,...,100},
   ytick={0,0.1,0.2,...,1},
   ]

   \addplot coordinates{(0,1.0) (10,0.96) (20,0.90) (30,0.85) (40,0.81)          
   (50,0.74) (60,0.70) (70,0.64) (80,0.59) (90,0.53) (100,0.47)};
   \addplot coordinates{(0,1.0) (10,0.93) (20,0.85) (30,0.78) (40,0.71)    
    (50,0.65) (60,0.57) (70,0.50) (80,0.43) (90,0.36) (100,0.29)};
    \addplot coordinates{(0,1.0) (10,0.92) (20,0.82) (30,0.70) (40,0.62)  
    (50,0.53) (60,0.43) (70,0.35) (80,0.26) (90,0.18) (100,0.11)};
    \legend{\emph{10 Nodes},\emph{8 Nodes},\emph{6 Nodes}}
    \end{axis}
    \end{tikzpicture}%
    ~%
    %
    \begin{tikzpicture}[baseline]
    \begin{axis}[
    xmax=100,xmin=0, 
    ymin= 0,ymax=1,
    xlabel=\emph{User Requests},ylabel=\emph{$E_{res}$ Percent},
    xtick={0,10,20,...,100},
    ytick={0,0.1,0.2,...,1},
    ]

    \addplot coordinates{(0,1.0) (10,0.94) (20,0.88) (30,0.82) (40,0.75)  
    (50,0.68) (60,0.62) (70,0.55) (80,0.49) (90,0.42) (100,0.34)};
    \addplot coordinates{(0,1.0) (10,0.92) (20,0.83) (30,0.74) (40,0.67)         
    (50,0.58) (60,0.50) (70,0.43) (80,0.35) (90,0.27) (100,0.19)};
    \addplot coordinates{(0,1.0) (10,0.90) (20,0.81) (30,0.70) (40,0.60)  
    (50,0.51) (60,0.42) (70,0.32) (80,0.23) (90,0.11) (100,0.04)};
    \legend{\emph{10 Nodes},\emph{8 Nodes},\emph{6 Nodes}}
    \end{axis}
    \end{tikzpicture}%
    ~%
    %
     \begin{tikzpicture}[baseline] 
     \begin{axis}[
     xmax=100,xmin=0,
     ymin= 0,ymax=1,
     xlabel=\emph{User Requests},ylabel=\emph{$E_{res}$ Percent},
     xtick={0,10,20,...,100},
     ytick={0,0.1,0.2,...,1},
     ]

     \addplot coordinates{(0,1.0) (10,0.93) (20,0.86) (30,0.81) (40,0.77)  
     (50,0.69) (60,0.60) (70,0.53) (80,0.45) (90,0.38) (100,0.32)};
     \addplot coordinates{(0,1.0) (10,0.91) (20,0.84) (30,0.77) (40,0.70) 
     (50,0.63) (60,0.53) (70,0.44) (80,0.35) (90,0.26) (100,0.18)};
     \addplot coordinates{(0,1.0) (10,0.89) (20,0.80) (30,0.72) (40,0.63) 
     (50,0.53) (60,0.46) (70,0.37) (80,0.28) (90,0.18) (100,0.11)};
     \legend{\emph{10 Nodes},\emph{8 Nodes},\emph{6 Nodes}}
     \end{axis} 
     \end{tikzpicture}

\end{document}

在此处输入图片描述

关键点是

  • 如果你写了类似的东西

    \结束{tikzpicture} \开始{tikzpicture}

你引入了一个“虚假”的空白。这是因为 TeX 会将其解释}<newline>}<white space>。你应该}%这样写,让 TeX 在此处丢弃换行符。事实上,这些虚假的空白是易碎空格,这些空格会导致不必要的换行。消除空格(%在行末使用)实际上可以达到您想要的效果。

  • 如果您需要更多空间,可以插入不间断空格(例如, )~。这就是我上面所做的。

  • 将多个轴放在一起很容易产生垂直的对齐问题。为了避免这些问题,我baseline为每个 添加了选项\begin{tikzpicture}。有关详细信息,请参阅pgfplots手册中的“对齐”部分。

  • 您可以使用\pgfplotsset来更改范围内所有图形的选项。“范围”是直到下一个\end{<...>}(或直到下一个匹配的})。在我们的上下文中,我曾经\pgfplotsset(重新)配置图例位置和绘图大小(参见下一项)。

  • 一般的最佳做法是不是缩放整个轴。虽然这是一种相对方便的减小尺寸的方法,但它也会产生不想要的效果,即缩放轴描述(字体)。缩放字体通常会导致文档外观不一致。最佳做法是缩放绘图尺寸,并对描述使用一致的字体大小。在这种情况下,我使用了预定义样式small(在手册部分“4.10 缩放选项”中描述pgfplots)来减小绘图和描述的大小。

    • 请注意,还提供了一个允许描述情节的pgfplots密钥。caption

您可以轻松看到我的结果也不适合该行。您需要根据自己的需要采用这些技巧——并且可能对子图(以及每个子图的多个标题\begin{figure}及其对齐方式)进行一些研究。

相关内容