如何将 tikz 图形作为节点包含到其他 tikz 环境中?

如何将 tikz 图形作为节点包含到其他 tikz 环境中?

我正在尝试将一个pgfplot图形作为节点包含在 tikz 环境中,或者通常,如何将其他 tikz 图形组合为节点以构成更大更复杂的图形。

更多细节:下图基本上是两个子图。我想让它们的高度相同,以便它们的 x 轴都在一条直线上(如您所见,图 B 比图 A 高一点);我无法做到这一点,所以我想如果我在新的 tikz 环境中将两个子图添加为两个节点,那么我可以通过像素精确定位它们。

在此处输入图片描述

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

答案1

轴来自pgfplotstikz 节点,因此您可以将其中几个放在同一个tikzpicture环境中,并使用通常的 tikz 定位方法定位它们。在下面的代码中,我们命名第一组轴,然后使用它创建的锚点来定位第二组轴。

您可以在 pgfplots 手册第 4.19 章中阅读有关定位的细微差别。

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

单个 tikzpicture 中有两个 pgfplots

答案2

您可以将tikzpictures内部迷你页面或 parbox 设置为固定高度和顶部对齐。这样,图表将垂直对齐。请参阅以下代码:

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

结果是(标题没有居中,因为我为了更好地显示效果减小了子图和小页面的宽度,只要你重新增加宽度,标题就会再次居中):

在此处输入图片描述

相关内容