2

I am trying to include a pgfplot figure as a node in a tikz environment, or in general, how to combine other tikz figures as nodes to make a bigger more complex one.

More Elaboration: The following picture is basically two sub figures. I want to make them at the same hight so that both of their x-axis are on one straightness, (As you can see Figure B is a bit higher than Figure A); and I couldn't do that so I think that If I added the two sub figures are two nodes in a new tikz environment, then I can position them accurately by pixel.

enter image description here

\documentclass[journal,10pt,twocolumns,letter]{IEEEtran}
\usepackage{geometry}%
\usepackage{subcaption}
\usepackage[dvipsnames, table]{xcolor}
\usepackage{tikz}
\usepackage{pgf}
\usepackage{pgfplots}


\begin{document}


\begin{figure}[!tb]
  \centering
  \begin{subfigure}[b]{0.6\linewidth}
  \centering
\begin{tikzpicture}[scale=0.7]
\begin{axis}[
    symbolic x coords={ X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11, X12, X13, X14, X15, X16 },
    xtick=data,
    xticklabel style = {font=\footnotesize},
    height=8cm,
    x=0.4cm,
    x tick label style={rotate=60, anchor=east},
    enlarge y limits=0.07,
  ]
    \addplot[ybar,color=black,mark=*, only marks,
        point meta=explicit symbolic] coordinates {
        (X1, 416428)
        (X2,  370116)
        (X3, 244583)
        (X4,     157880)
        (X5,     143148)
        (X6,     119286)
        (X7,     79560)
        (X8,     63613)
        (X9,     59370)
        (X10,     28768)
        (X11,     18062)
        (X12,     7808)
        (X13,     7468)
        (X14,     5144)
        (X15,     799 )
        (X16,     288 )
    };
\end{axis}
\end{tikzpicture}


    \caption{SubFigure A}
  \end{subfigure}%
  \begin{subfigure}[b]{0.4\linewidth}
  \centering
\begin{tikzpicture}[scale=0.7]
    \begin{axis}[
            symbolic x coords={ABCDE1, ABCDE4, ABCDE3, ABCDE2},
            xtick=data,
            xticklabel style = {font=\footnotesize},
            height=8cm,
            x=0.5cm,
            enlarge x limits=0.3,
            enlarge y limits=0.07,
            nodes near coords,
            x tick label style={rotate=60, anchor=east},
            y tick label style={scaled ticks=base 10:-3},
            ymin=0,
          ]

      \addplot[mark=*, only marks,
        point meta=explicit symbolic] coordinates {
        (ABCDE4,    5418)
        (ABCDE3, 5378)
        (ABCDE2,     1397)
        (ABCDE1,     5550)
    };

    \end{axis}%
\end{tikzpicture}%

    \caption{Subfigure B}
  \end{subfigure}
\end{figure}



\end{document}
  • 3
    Nesting tikz pictures is A Bad Idea. There are several questions on this site that give methods of avoiding it. Could you add some example code of what you are trying to do so that we can best help you decide which method will suit you? – Loop Space Aug 23 '18 at 20:03
  • I have added the full details, @LoopSpace . – Taha Magdy Aug 23 '18 at 20:33
4

Axes from pgfplots are tikz nodes, so you can put several of them in the same tikzpicture environment and position them using the usual tikz positioning methods. In the following code, we name the first set of axes and then use the anchors that it creates to position the second set of axes.

You can read about the subtleties of positioning in Chapter 4.19 of the pgfplots manual.

\documentclass[journal,10pt,twocolumns,letter]{IEEEtran}
%\url{https://tex.stackexchange.com/q/447396/86}
\usepackage{geometry}%
\usepackage{subcaption}
\usepackage[dvipsnames, table]{xcolor}
\usepackage{tikz}
\usepackage{pgf}
\usepackage{pgfplots}
\usetikzlibrary{calc}

\begin{document}


\begin{figure}[!tb]
  \centering
\begin{tikzpicture}[scale=0.7]
\begin{axis}[
    symbolic x coords={ X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11, X12, X13, X14, X15, X16 },
    xtick=data,
    xticklabel style = {font=\footnotesize},
    height=8cm,
    x=0.4cm,
    x tick label style={rotate=60, anchor=east},
    enlarge y limits=0.07,
    name=left plot,
    title=Figure A,
    title style={at={(0.5,-.4)}}
  ]
    \addplot[ybar,color=black,mark=*, only marks,
        point meta=explicit symbolic] coordinates {
        (X1, 416428)
        (X2,  370116)
        (X3, 244583)
        (X4,     157880)
        (X5,     143148)
        (X6,     119286)
        (X7,     79560)
        (X8,     63613)
        (X9,     59370)
        (X10,     28768)
        (X11,     18062)
        (X12,     7808)
        (X13,     7468)
        (X14,     5144)
        (X15,     799 )
        (X16,     288 )
    };
\end{axis}
    \begin{axis}[
            symbolic x coords={ABCDE1, ABCDE4, ABCDE3, ABCDE2},
            xtick=data,
            xticklabel style = {font=\footnotesize},
            height=8cm,
            x=0.5cm,
            enlarge x limits=0.3,
            enlarge y limits=0.07,
            nodes near coords,
            x tick label style={rotate=60, anchor=east},
            y tick label style={scaled ticks=base 10:-3},
            ymin=0,
            at={($(left plot.south east)+(4cm,0)$)},
            title=Figure B,
            title style={at={(0.5,-.4)}}
          ]

      \addplot[mark=*, only marks,
        point meta=explicit symbolic] coordinates {
        (ABCDE4,    5418)
        (ABCDE3, 5378)
        (ABCDE2,     1397)
        (ABCDE1,     5550)
    };

    \end{axis}%
\end{tikzpicture}%

\end{figure}



\end{document}

two pgfplots in a single tikzpicture

3

You can place the tikzpictures inside minipages or parboxes with fixed height and top alignment. This way, graphs will be aligned vertically. See the following code:

\documentclass[journal,10pt,twocolumns,letter]{IEEEtran}
\usepackage{geometry}
\usepackage{subcaption}
\usepackage[dvipsnames, table]{xcolor}
\usepackage{pgf}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}

\begin{document}

\begin{figure}[!tb]
  \centering
  \begin{subfigure}[b]{0.4\textwidth}
  \centering
  \begin{minipage}[t][60mm][t]{0.4\textwidth}
  \begin{tikzpicture}[scale=0.7]
  \begin{axis}[
    symbolic x coords={X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11, X12, X13, X14, X15, X16},
    xtick=data,
    xticklabel style={font=\footnotesize},
    height=8cm,
    x=0.4cm,
    x tick label style={rotate=60, anchor=east},
    enlarge y limits=0.07
  ]
  \addplot[ybar, color=black, mark=*, only marks,
    point meta=explicit symbolic] coordinates {
      (X1,  416428)
      (X2,  370116)
      (X3,  244583)
      (X4,  157880)
      (X5,  143148)
      (X6,  119286)
      (X7,   79560)
      (X8,   63613)
      (X9,   59370)
      (X10,  28768)
      (X11,  18062)
      (X12,   7808)
      (X13,   7468)
      (X14,   5144)
      (X15,    799)
      (X16,    288)
    };
  \end{axis}
  \end{tikzpicture}
  \end{minipage}
  \caption{SubFigure A}
  \end{subfigure}%
%
  \begin{subfigure}[b]{0.4\textwidth}
  \centering
  \begin{minipage}[t][60mm][t]{0.4\textwidth}
  \begin{tikzpicture}[scale=0.7]
  \begin{axis}[
    symbolic x coords={ABCDE1, ABCDE4, ABCDE3, ABCDE2},
    xtick=data,
    xticklabel style={font=\footnotesize},
    height=8cm,
    x=0.5cm,
    enlarge x limits=0.3,
    enlarge y limits=0.07,
    nodes near coords,
    x tick label style={rotate=60, anchor=east},
    y tick label style={scaled ticks=base 10:-3},
    ymin=0,
  ]
  \addplot[mark=*, only marks,
    point meta=explicit symbolic] coordinates {
      (ABCDE4, 5418)
      (ABCDE3, 5378)
      (ABCDE2, 1397)
      (ABCDE1, 5550)
    };
  \end{axis}
  \end{tikzpicture}
  \end{minipage}
  \caption{Subfigure B}
  \end{subfigure}
\end{figure}

\end{document}

Which yields (the captions are not centered because I decreased the width of the subfigures and minipages in order to better show the effect, as soon as you re-increase the width, the captions will be centered again):

enter image description here

  • What are those numbers [60mm][t]{0.4\textwidth? and how do you tune them, I mean how to choose those number correctly? – Taha Magdy Aug 23 '18 at 23:11
  • 1
    The minipages should have the same width (0.4\textwidth) like the subfigures and the width (60mm) should be the same as or a bit more than the tikzpictures. You have to adjust these numbers manually. – Jasper Habicht Aug 24 '18 at 5:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.