1

I have read a couple of questions that are quite close to mine, but I have no idea how to incorporate them in my code. I have two figures and I would like to have them side by side. Could you give me a hand with this? Thanks

\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}
  • 4
    Empty lines indicate paragraph breaks, remove them. – Torbjørn T. May 18 '18 at 19:24
  • 1
    Depending on your final goal you may want to put them into \subfigures inside a figure environment. – user121799 May 18 '18 at 20:10
  • 1
    @marmot I followed on your proposal. – Dr. Manuel Kuehner May 18 '18 at 21:07
2
  • Based on Text placed under figure instead of above and Manually Place a Figure in LaTeX (here: End of Chapter/Section).
  • I put you pgfplots stuff in external files in order to make the structure of the code clearer (in general a good idea in my opinion).
  • Here I use the float package in order to place the float manually (\begin{figure}[H]) at a specific position (there are opinions against that solution).
  • You have a weird mixture of absolute and relative dimensions (e. g. bar width=.8cm and height=.5\textwidth). I did not optimize that - I just changed it a bit until the MWE was ok-ish.
  • I also show how to reference the figures (\ref and \label).

Main Code

\usepackage{subcaption} is essential.

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

Full Code

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

enter image description here

  • That's exactly what I was looking for. Thanks @Dr.ManuelKuehner. Anyway, how could I change the size of the numbers upper part of the bars? – little_mice May 18 '18 at 21:47
  • This is a different question and I am already in bed :) – Dr. Manuel Kuehner May 18 '18 at 21:53
  • Ohhh Do not worry @Dr.ManuelKuehner. Anyway, thanks for the first answer – little_mice May 18 '18 at 22:01
  • 2
    @little_mice You could just do every node near coord/.append style={rotate=90, anchor=west, font=\small\sffamily},, see here. If you drop the rotate and anchor stuff, it won't get rotated, but probably the text will be too wide then. – user121799 May 18 '18 at 22:03

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.