两个相邻的图形(Tikz)

两个相邻的图形(Tikz)

我读过几个与我的问题非常接近的问题,但我不知道如何将它们合并到我的代码中。我有两个图,我想将它们并排显示。你能帮我一下吗?谢谢

\documentclass{article}

\usepackage{pgfplots}
\usepackage{tikz}
\usetikzlibrary{patterns}

\pgfplotsset{compat=1.11,
/pgfplots/ybar legend/.style={
/pgfplots/legend image code/.code={%
   \draw[##1,/tikz/.cd,yshift=-0.25em]
    (0cm,0cm) rectangle (3pt,0.8em);},
},
}

\begin{document}

\pgfplotstableread[row sep=\\,col sep=&]{
interval & Eval & Dev \\
Mono     & 19.29  & 31.31 \\
Tri1     & 9.56 & 15.97  \\
Tri2    & 8.16 & 13.86 \\
Tri3   & 10 & 10 \\
}\mydata

\begin{tikzpicture}
\begin{axis}[
        ybar,
        bar width=.8cm, %ancho barra
        width=9cm, %dimensiones de figura
        height=.5\textwidth,
        legend style={at={(1,1)},
            anchor=north east,legend columns=-1},
        symbolic x coords={Mono, Tri1, Tri2, Tri3},
        xtick=data,
        nodes near coords, %letricas arriba
        nodes near coords align={vertical},
        ymin=0,ymax=35,
        ylabel={WER (\%)},
    ]
    \addplot [draw=black, fill=black] table[x=interval,y=Eval]{\mydata};
    \addplot [draw=black, pattern color = black, pattern = north west lines] table[x=interval,y=Dev]{\mydata};
    \legend{Eval, Dev}
\end{axis}
\node[above,font=\large\bfseries] at (current bounding box.north) {EN - Tg};
\end{tikzpicture}

\pgfplotstableread[row sep=\\,col sep=&]{
interval & Eval & Dev \\
Mono     & 20.87  & 32.70 \\
Tri1     & 10.22 & 16.26  \\
Tri2    & 8.38 & 14.87 \\
Tri3   & 10 & 10 \\
}\mydata

\begin{tikzpicture}
\begin{axis}[
        ybar,
        bar width=.8cm, %ancho barra
        width=9cm, %dimensiones de figura
        height=.5\textwidth,
        legend style={at={(1,1)},
            anchor=north east,legend columns=-1},
        symbolic x coords={Mono, Tri1, Tri2, Tri3},
        xtick=data,
        nodes near coords, %letricas arriba
        nodes near coords align={vertical},
        ymin=0,ymax=38,
        ylabel={WER (\%)},
    ]
    \addplot [draw=black, fill=black] table[x=interval,y=Eval]{\mydata};
    \addplot [draw=black, pattern color = black, pattern = north west lines] table[x=interval,y=Dev]{\mydata};
    \legend{Eval, Dev}
\end{axis}
\node[above,font=\large\bfseries] at (current bounding box.north) {EN - Tgpr};
\end{tikzpicture}

\end{document}

答案1

  • 基于文字置于图下而不是图上在 LaTeX 中手动放置图形(此处:章节/部分末尾)
  • 我把你的pgfplots东西放在外部文件中,以使代码结构更清晰(在我看来这是一个好主意)。
  • 在这里我使用float包来手动将浮动(\begin{figure}[H])放置在特定位置(有意见反对该解决方案)。
  • 您有一个奇怪的绝对尺寸和相对尺寸混合(例如bar width=.8cmheight=.5\textwidth)。我没有优化它 - 我只是做了一些修改,直到 MWE 还可以。
  • 我还展示了如何引用图形(\ref\label)。

主要代码

\usepackage{subcaption}是必不可少的。

\begin{figure}[H]
    \centering
    \begin{subfigure}[t]{0.5\textwidth}
        \centering
        \input{pgfplots__Diagram__1.tex}
        \caption{Subcaption of the left diagram.}
        \label{Fig:SubLeft}
    \end{subfigure}%
    ~ 
    \begin{subfigure}[t]{0.5\textwidth}
        \centering
        \input{pgfplots__Diagram__2.tex}
        \caption{Subcaption of the right diagram.}
        \label{Fig:SubRight}
    \end{subfigure}
    \caption{Caption of both diagrams.}
    \label{Fig:Main}
\end{figure}

完整代码

\documentclass{article}

\usepackage{filecontents}

%%%
\begin{filecontents}{pgfplots__Diagram__1.tex}
%
    \pgfplotstableread[row sep=\\,col sep=&]{
    interval & Eval & Dev \\
    Mono     & 19.29  & 31.31 \\
    Tri1     & 9.56 & 15.97  \\
    Tri2    & 8.16 & 13.86 \\
    Tri3   & 10 & 10 \\
    }\myDataDiagramOne
    %
    \begin{tikzpicture}
    \begin{axis}[
            ybar,
            bar width=4mm, 
            width=0.95\textwidth,
            height=50mm,
            legend style={at={(1,1)},
                anchor=north east,legend columns=-1},
            symbolic x coords={Mono, Tri1, Tri2, Tri3},
            xtick=data,
            nodes near coords, %letricas arriba
            nodes near coords align={vertical},
            ymin=0,ymax=35,
            ylabel={WER (\%)},
        ]
        \addplot [draw=black, fill=black] table[x=interval,y=Eval]{\myDataDiagramOne};
        \addplot [draw=black, pattern color = black, pattern = north west lines] table[x=interval,y=Dev]{\myDataDiagramOne};
        \legend{Eval, Dev}
    \end{axis}
    \node[above,font=\large\bfseries] at (current bounding box.north) {EN - Tg};
    \end{tikzpicture}
%
\end{filecontents}

%%%
\begin{filecontents}{pgfplots__Diagram__2.tex}
%
    \pgfplotstableread[row sep=\\,col sep=&]{
    interval & Eval & Dev \\
    Mono     & 20.87  & 32.70 \\
    Tri1     & 10.22 & 16.26  \\
    Tri2    & 8.38 & 14.87 \\
    Tri3   & 10 & 10 \\
    }\myDataDiagramTwo
    %
    \begin{tikzpicture}
    \begin{axis}[
            ybar,
            bar width=4mm, 
            width=0.95\textwidth,
            height=50mm,
            legend style={at={(1,1)},
                anchor=north east,legend columns=-1},
            symbolic x coords={Mono, Tri1, Tri2, Tri3},
            xtick=data,
            nodes near coords, %letricas arriba
            nodes near coords align={vertical},
            ymin=0,ymax=38,
            ylabel={WER (\%)},
        ]
        \addplot [draw=black, fill=black] table[x=interval,y=Eval]{\myDataDiagramTwo};
        \addplot [draw=black, pattern color = black, pattern = north west lines] table[x=interval,y=Dev]{\myDataDiagramTwo};
        \legend{Eval, Dev}
    \end{axis}
    \node[above,font=\large\bfseries] at (current bounding box.north) {EN - Tgpr};
    \end{tikzpicture}
%
\end{filecontents}

\usepackage{pgfplots}
\usepackage{tikz}
\usetikzlibrary{patterns}

% https://tex.stackexchange.com/questions/376318
\usepackage{subcaption}
\usepackage{float}


\begin{document}

Text before.

\begin{figure}[H]
    \centering
    \begin{subfigure}[t]{0.5\textwidth}
        \centering
        \input{pgfplots__Diagram__1.tex}
        \caption{Subcaption of the left diagram.}
        \label{Fig:SubLeft}
    \end{subfigure}%
    ~ 
    \begin{subfigure}[t]{0.5\textwidth}
        \centering
        \input{pgfplots__Diagram__2.tex}
        \caption{Subcaption of the right diagram.}
        \label{Fig:SubRight}
    \end{subfigure}
    \caption{Caption of both diagrams.}
    \label{Fig:Main}
\end{figure}

Text after. In \figurename~\ref{Fig:Main} there are two subfigures, nameley Subfigure~\ref{Fig:SubLeft} and \ref{Fig:SubRight}.

\end{document}

在此处输入图片描述

相关内容