8

I'm trying to generate a more complex figure by tikz. But it has some bugs. I don't know why. The code is as follows:

\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, i.e., delay.txt and hit.txt are as following:

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

I think there exist some bugs in the following code, but I don't know why!

   [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]

The result most like:

enter image description here

Can you help me? Thank you!

3
  • Please make your example compilable (\documentclass is missing) and explain what is the problem here. Why do you think there are some bugs? Erros, warnings, crashes...?
    – LaRiFaRi
    Sep 19, 2016 at 14:17
  • A couple of unrelated comments: i) $Expiration\ Time\ (s)$ is not math but text, so don't use math mode: Expiration Time (s). Same for the ylabel. If you want italic, use \textit{...}, or add font=\itshape to the xlabel style. ii) You generally don't want to have the pdftex option for graphicx, so just use \usepackage{graphicx}. The package figures that one out by itself. Sep 19, 2016 at 14:35
  • Thank you for your answers! I am a new learner. I will pay attention to the specifications. Thank you!
    – Sean
    Sep 21, 2016 at 7:56

2 Answers 2

4

Here is a solution using \savebox to avoid peculiar interferences arising from nesting and scaling; this also allows us to position the inserted axis in the scope of the outer axis with axis cs coordinates. Note that for asymmetric scaling (rectangular plots) we cannot use xscale and yscale as offered by tikz, since label positions don't end up where one would expect. Instead, we use unit vector ratio and then do symmetric canvas scaling (= scaling including also the text).

A sketch of the construction:

% 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}

Here are two examples: a simple one with plots defined by expressions, and the example of the original post reading the data from tables.

\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}

enter image description here

\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}

enter image description here

7
  • Thank you for your answer, you are very patient! What you show is also work in my computer. But when there is a input from .txt, it doesn't work. Maybe I place the input '\addplot table {delay.txt};' in a wrong place? I placed it after '\mypic{}' and before '{\usebox\mybox};'. Maybe it can work for a formula, but not to a input from XXX.txt document?
    – Sean
    Sep 26, 2016 at 9:09
  • @Sean Good point. I have now added your original example. It also works for input from files :-)
    – gernot
    Sep 26, 2016 at 9:42
  • @Sean I have rewritten and simplified the description of the solution. If you find that it works, consider ticking my second answer instead of the first one. I think it is the more robust one.
    – gernot
    Sep 26, 2016 at 10:34
  • Thank you! The picture you show is what I want! But there is a error in my computer, i.e., ! Package pgfkeys Error: Choice '1.14' unknown in key '/pgfplots/compat/anchors' . I searched it in the network. It shows that maybe the pgfplots version is old. link But the .log shows 'Package: pgfplots 2011/12/29 v1.5.1 (git show 1.5.1-4-g53e640f )'. The version is v1.5.1, and higher than v1.14. I have no idea what's wrong?
    – Sean
    Sep 28, 2016 at 2:44
  • @Sean Remove \pgfplotsset{compat=1.14} and see if you get the same picture as above. This command just indicates that this picture works with version 1.14. If you compile the document with a later version of pgfplots, it will switch back to this version such that the document still works even if some features have changed. But it may well be that your version of pgfplots is sufficient. If the picture comes out differently or you get other error messages with the code above, than you should upgrade your pgfplots (which is no bad idea anyway, it is quite old).
    – gernot
    Sep 28, 2016 at 5:57
8

The problem is in the line \begin{axis}[scale=0.2][ (apart from the missing \documentclass).

First, the axis environment has only one set of options, so you have to replace ][ by ,.

Second, scale transforms only the drawing, not the labels etc. Therefore use transform canvas={scale=0.2} instead.

With

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

instead of

\begin{axis}[scale=0.2][

you get

enter image description here

11
  • I would also suggest not adding the small axis inside a node, instead use at={(x,y)} in the axis options`. Sep 19, 2016 at 14:32
  • Thank you for your answers! It's really what I want! I have another question. How can I change the shape of the embedded (small) picture? I.e., it is a square at this moment, how can I change it to be rectangular? While the larger picture is still keep as a square.
    – Sean
    Sep 20, 2016 at 2:37
  • @Sean See the discussion on Rectangular graphs in pgfplots. If you just want to have asymmetric scaling, you can replace scale=0.2 by xscale=0.2,yscale=0.3, but this will distort letters. Or you use scaling outside of transform canvas, then the graphical elements will be stretched, but the letters remain the same. Try e.g. xscale=1.5 as an additional option of axis.
    – gernot
    Sep 20, 2016 at 10:40
  • Thank you for your answers. I have tried the second method. The letters is not change (I also tried the first method you teach me, the letters have distorted). But there still exist a problem, i.e., the xlable is on the right side, not the middle. Even if I have use 'xlabel style={align=center}' . (The first method doesn't change the position of xlable. The xlable keep in the middle.) Can you help me? Thank you!
    – Sean
    Sep 21, 2016 at 7:50
  • @Sean There seems to be a problem in the interaction of label positioning with scaling. Try the option unit vector ratio=... with a value different from 1 1 together with canvas scaling. For an example of this option, see this answer to "Rectangular graphs in pgfplots".
    – gernot
    Sep 21, 2016 at 8:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .