pgfplots:如何嵌套和缩放轴环境

pgfplots:如何嵌套和缩放轴环境

我正在尝试用 tikz 生成更复杂的图形。但是它有一些错误。我不知道为什么。代码如下:

\usepackage[pdftex]{graphicx}
\usepackage{amsmath,environ}
\usepackage[caption=false,font=footnotesize]{subfig}
\usepackage{caption}
\usepackage{pgfplots}

\begin{document}

\begin{figure*}[!tp]

\begin{minipage}{.49\textwidth}
\begin{tikzpicture}[scale=0.5]
\begin{axis}[
    xlabel style={align=center},
    xlabel=$Expiration\ Time\ (s)$ \\(a),
    ylabel style={align=center},
    ylabel=$Reduced\ RTT\ Ratio\ (\%)$,
    xmin=10, xmax=60,
    ymin=75, ymax=95,
    legend style={nodes=right},
    legend pos= south east]
\addplot table {delay.txt};
\addlegendentry{10000}
\end{axis}
\draw(0.1,0.1) node{ 
\begin{axis}[scale=0.2][
    xlabel style={align=center},
    xlabel=$Expiration\ Time\ (s)$ \\(b),
    xlabel style={align=center},
    ylabel=$Cache\ Hit\ Rate\ (\%)$,
    xmin=10, xmax=60,
    ymin=80, ymax=100,
    legend style={nodes=right},
    legend pos= south east]
\addplot table {hit.txt};
\addlegendentry{10000}
\end{axis}};
\end{tikzpicture}
\caption{Effects of expiration time of cache entry in delegates \newline}
\label{figure 4}
\end{minipage}
\end{figure*}

\end{document}

Date,即delay.txt和hit.txt内容如下:

delay.txt:
    10  83.70616734
    20  87.78888284
    30  89.30183444
    40  90.08903274
    50  90.58099978
    60  90.92872104

hit.txt:
    10  90.98347396
    20  95.0861077
    30  96.60898943
    40  97.3979899
    50  97.89094057   
    60  98.23992643

我认为以下代码存在一些错误,但我不知道为什么!

   [xlabel style={align=center},
    xlabel=$Expiration\ Time\ (s)$ \\(b),
    xlabel style={align=center},
    ylabel=$Cache\ Hit\ Rate\ (\%)$,
    xmin=10, xmax=60,
    ymin=80, ymax=100,
    legend style={nodes=right},
    legend pos= south east]

最喜欢的结果:

在此处输入图片描述

你能帮助我吗?谢谢!

答案1

这里有一个解决方案,使用\savebox来避免嵌套和缩放引起的特殊干扰;这也使我们能够将插入的定位在具有坐标axis的外部范围内。请注意,对于非对称缩放(矩形图),我们不能使用提供的和,因为标签位置不会出现在预期的位置。相反,我们使用,然后进行对称画布缩放(= 也包括文本的缩放)。axisaxis csxscaleyscaletikzunit vector ratio

施工草图:

% First construct the graph to be inserted and save it
\newsavebox\insertedgraph
\savebox\insertedgraph
  {\begin{tikzpicture}
     \begin{axis}[unit vector ratio=..., xmin=..., xmax=...]
     ...
     \end{axis}
   \end{tikzpicture}%
  }

% Now for the main graph
\begin{tikzpicture}
  \begin{axis}
  ... \addplot ... whatever ...
  % Now we insert the other graph at a reduced size
  \node at (axis cs: ...position ...)
    {\scalebox{... scaling factor ...}{\usebox\insertedgraph}};
  \end{axis}
\end{tikzpicture}

这里有两个示例:一个是带有由表达式定义的图表的简单示例,另一个是从表读取数据的原始帖子的示例。

\documentclass{standalone}
\usepackage{pgfplots}\pgfplotsset{compat=1.14}
\begin{document}
\newsavebox\insertedgraph
\savebox\insertedgraph
  {\begin{tikzpicture}
     \begin{axis}[unit vector ratio=1 2,xmin=0,xmax=5]
       \addplot[domain=0.1:5] {1/x};
     \end{axis}
   \end{tikzpicture}%
  }
\begin{tikzpicture}
  \begin{axis}
    \addplot[domain=-5:5] {x^2};
    \node at (axis cs:-0.2,15) {\scalebox{0.5}{\usebox\insertedgraph}};
  \end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

\begin{filecontents}{delay.txt}
    10  83.70616734
    20  87.78888284
    30  89.30183444
    40  90.08903274
    50  90.58099978
    60  90.92872104
\end{filecontents}
\begin{filecontents}{hit.txt}
    10  90.98347396
    20  95.0861077
    30  96.60898943
    40  97.3979899
    50  97.89094057   
    60  98.23992643
\end{filecontents}
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\begin{document}
\newsavebox\mybox
\savebox\mybox
  {\begin{tikzpicture}
    \begin{axis}[
    unit vector ratio=3 2,
    xlabel style={align=center},
    xlabel=Expiration Time (s)\\(b),
    xlabel style={align=center},
    ylabel=Cache\ Hit Rate (\%),
    xmin=10, xmax=60,
    ymin=80, ymax=100,
    legend style={nodes=right},
    legend pos= south east]
    \addplot table {hit.txt};
    \addlegendentry{10000}
    \end{axis}
  \end{tikzpicture}%
  }

\begin{tikzpicture}
\begin{axis}[
    xlabel style={align=center},
    xlabel=Expiration Time (s)\\(a),
    ylabel style={align=center},
    ylabel=Reduced RTT Ratio\ (\%),
    xmin=10, xmax=60,
    ymin=75, ymax=95,
    legend style={nodes=right},
    legend pos= south east]
\addplot table {delay.txt};
\addlegendentry{10000}
\node at (axis cs:35,81) {\scalebox{0.4}{\usebox\mybox}}; 
\end{axis}
\end{tikzpicture}

\end{document}

在此处输入图片描述

答案2

问题出在线上\begin{axis}[scale=0.2][(除了缺少\documentclass)。

首先,axis环境只有一组选项,因此您必须用][替换,

其次,scale仅转换绘图,而不转换标签等。因此请使用transform canvas={scale=0.2}

\begin{axis}[transform canvas={scale=0.2},

代替

\begin{axis}[scale=0.2][

你得到

在此处输入图片描述

相关内容