3

I have 4 plots that should be placed on the same page. Does anyone know how to do it? I need to have a caption/title under each plot (and I cannot place it above the plot as there are numbers), a legend can be common to all plots.

Here is the code:

\begin{figure}
\begin{tikzpicture}
\begin{axis}[
    axis lines*=left,
    ybar,
    ymin=0,ymax=1,
    enlarge x limits=0.5,
    bar width=10pt,
    legend style={at={(0.5,-0.15)},
      anchor=north,legend columns=-1},
    ylabel={percentage, \%},
    symbolic x coords={P,R},
    xtick=data,
    major x tick style = transparent,
    ymajorgrids = true,
    tick align=outside,
    nodes near coords,
    every node near coord/.append style={anchor=mid west, rotate=90},  
 %   nodes near coords,
  %  nodes near coords align={horizontal},
    ]
\addplot [fill=red!50]coordinates {(P,0.4) (R,0.3)  };
\addplot [fill=red!50]coordinates {(P,0.5) (R,0.5)};
legend{min1,min2}
\end{axis}
\end{tikzpicture}
\caption{Results P} 
\label{fig:3}
\end{figure}



\begin{figure}
\begin{tikzpicture}
\begin{axis}[
    axis lines*=left,
    ybar,
    ymin=0,ymax=1,
    enlarge x limits=0.5,
    bar width=10pt,
    legend style={at={(0.5,-0.15)},
      anchor=north,legend columns=-1},
    ylabel={percentage, \%},
    symbolic x coords={P,R},
    xtick=data,
    major x tick style = transparent,
    ymajorgrids = true,
    tick align=outside,
    nodes near coords,
    every node near coord/.append style={anchor=mid west, rotate=90},  
 %   nodes near coords,
  %  nodes near coords align={horizontal},
    ]
\addplot [fill=green!50]coordinates {(P,0.94) (R,0.5)  };
\addplot [fill=violet!50]coordinates {(P,0.6) (R,0.6)};
\legend{min1,min2}
\end{axis}
\end{tikzpicture}
\caption{Results I} 
\label{fig:4}
\end{figure}


\begin{figure}
\begin{tikzpicture}
\begin{axis}[
    axis lines*=left,
    ybar,
    ymin=0,ymax=1,
    enlarge x limits=0.5,
    bar width=10pt,
    legend style={at={(0.5,-0.15)},
      anchor=north,legend columns=-1},
    ylabel={percentage, \%},
    symbolic x coords={P,R},
    xtick=data,
    major x tick style = transparent,
    ymajorgrids = true,
    tick align=outside,
    nodes near coords,
    every node near coord/.append style={anchor=mid west, rotate=90},  
 %   nodes near coords,
  %  nodes near coords align={horizontal},
    ]
\addplot [fill=green!50]coordinates {(P,0.5) (R,0.7)  };
\addplot [fill=violet!50]coordinates {(P,0.4) (R,0.9)};
legend{min1,min2}
\end{axis}
\end{tikzpicture}
\caption{Results U} 
\label{fig:5}
\end{figure}


\begin{figure}
\begin{tikzpicture}
\begin{axis}[
    axis lines*=left,
    ybar,
    ymin=0,ymax=1,
    enlarge x limits=0.5,
    bar width=10pt,
    legend style={at={(0.5,-0.15)},
      anchor=north,legend columns=-1},
    ylabel={percentage, \%},
    symbolic x coords={P,R},
    xtick=data,
    major x tick style = transparent,
    ymajorgrids = true,
    tick align=outside,
    nodes near coords,
    every node near coord/.append style={anchor=mid west, rotate=90},  
 %   nodes near coords,
  %  nodes near coords align={horizontal},
    ]
\addplot [fill=green!50]coordinates {(P,0.1) (R,0.6)  };
\addplot [fill=green!50]coordinates {(P,0.2) (R,0.9)};
legend{min1,min2}
\end{axis}
\end{tikzpicture}
\caption{Results X} 
\label{fig:6}
\end{figure}

I would appreciate any suggestions! I tried \matrix environment but cannot add a caption into each plot. A general title to all 4 plots would be nice as well.

Torbjørn T.
  • 206,688
tutur
  • 33
  • Welcome to TeX.SX! Please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem. – Paul Gessler Feb 03 '14 at 16:08
  • Possible duplicate or helpful answers: Two figures side by side – Paul Gessler Feb 03 '14 at 16:13

1 Answers1

2

Here's an option using the subcaption package

screenshot

I have also taken the liberty to introduce a style called, mystyle, which avoids having to repeat too much of the code.

% arara: pdflatex
\documentclass{article}
\usepackage{pgfplots}
\usepackage{subcaption}

\pgfplotsset{
every axis/.append style={width=\textwidth},
mystyle/.style={
axis lines*=left,
    ybar,
    ymin=0,ymax=1,
    enlarge x limits=0.5,
    bar width=10pt,
    legend style={at={(0.5,-0.15)},
      anchor=north,legend columns=-1},
    ylabel={percentage, \%},
    symbolic x coords={P,R},
    xtick=data,
    major x tick style = transparent,
    ymajorgrids = true,
    tick align=outside,
    nodes near coords,
    every node near coord/.append style={anchor=mid west, rotate=90},  
}
}
\begin{document}

\begin{figure}
\begin{subfigure}[b]{.5\textwidth}
\begin{tikzpicture}
\begin{axis}[
    mystyle
    ]
\addplot [fill=red!50]coordinates {(P,0.4) (R,0.3)  };
\addplot [fill=red!50]coordinates {(P,0.5) (R,0.5)};
\legend{min1,min2}
\end{axis}
\end{tikzpicture}
\caption{Results P} 
\label{fig:3}
\end{subfigure}%
\begin{subfigure}[b]{.5\textwidth}
\begin{tikzpicture}
\begin{axis}[
    mystyle,
 %   nodes near coords,
  %  nodes near coords align={horizontal},
    ]
\addplot [fill=green!50]coordinates {(P,0.94) (R,0.5)  };
\addplot [fill=violet!50]coordinates {(P,0.6) (R,0.6)};
\legend{min1,min2}
\end{axis}
\end{tikzpicture}
\caption{Results I} 
\label{fig:4}
\end{subfigure}

\begin{subfigure}[b]{.5\textwidth}
\begin{tikzpicture}
\begin{axis}[
    mystyle
    ]
\addplot [fill=green!50]coordinates {(P,0.5) (R,0.7)  };
\addplot [fill=violet!50]coordinates {(P,0.4) (R,0.9)};
\legend{min1,min2}
\end{axis}
\end{tikzpicture}
\caption{Results U} 
\label{fig:5}
\end{subfigure}%
\begin{subfigure}[b]{.5\textwidth}
\begin{tikzpicture}
\begin{axis}[
    mystyle
    ]
\addplot [fill=green!50]coordinates {(P,0.1) (R,0.6)  };
\addplot [fill=green!50]coordinates {(P,0.2) (R,0.9)};
\legend{min1,min2}
\end{axis}
\end{tikzpicture}
\caption{Results X} 
\label{fig:6}
\end{subfigure}
\caption{Global caption goes here}
\end{figure}
\end{document}
cmhughes
  • 100,947
  • Hi, Thank you very much for help! It works nicely. Do you know any trick how to make a single legend (with 2 criteria min1, min2) for all plots in case the colors in the plots are the same? So it does not show the same thing under each subfigure... – tutur Feb 04 '14 at 16:57