How to place 3 diagrams next to each other

minipagesubfloats

I am trying to put three diagrams next to each other, but it does not work. If I use subfigure the third figure is below the others, even if there is enough space for all three in a row. Now I tried to include minipages, but I am not sure how to position the diagrams next to each other now:

\documentclass{scrartcl}
\usepackage[margin=0.5in]{geometry}
\usepackage{textcomp}
\usepackage{pgfplots}
\usepackage{subfig}
\usepackage{graphicx}
\usepackage{subcaption}
\captionsetup{compatibility=false}
\pgfplotsset{width=5cm,compat=1.9}


\begin{document}

\begin{figure}[htbp]

%Start figure 1
     \begin{minipage}{0.45\textwidth}{
            \begin{tikzpicture}
\begin{axis}[
    xlabel={Capacity},
    ylabel={Service Level in \%},
    xmin=0, xmax=8,
    ymin=0, ymax=110,
    xtick={0,1,2,3,4,5,6,7,8},
    ytick={0,20,40,60,80,100},
    legend pos=north west,
    ymajorgrids=true,
    grid style=dashed,
]
\addplot[
    color=blue,line width=0.5mm,
    mark=square,
    ]
    coordinates {
    (2,90.92)(4,97.65)(6,99.94 )(8,100 )(10,100)
    };
\end{axis};
                \end{tikzpicture}}
    \end{minipage}\hfill
    


%Start figure 2
     \begin{minipage}{0.45\textwidth}
     {\begin{tikzpicture}
\begin{axis}[
    xlabel={Capacity},
    ylabel={Costs in €},
    xmin=0, xmax=8,
    ymin=0, ymax=1000,
    xtick={0,1,2,3,4,5,6,7,8},
    ytick={0,100,200,300,400,500,600,700,800,900,1000},
    legend pos=north west,
    ymajorgrids=true,
    grid style=dashed,
]

\addplot[
    color=blue,line width=0.5mm,
    mark=square,
    ]
    coordinates {
    (2,909.83)(4,740.87)(6,740.87)(8,740.87)(10,740.87)
    };
\end{axis}
\end{tikzpicture}}
\end{minipage}\hfill


    
    
    
%Start figure 3
     \begin{minipage}{0.45\textwidth}{
     \begin{tikzpicture}
\begin{axis}[
    title={Sensitivity Analysis Service Level vs Costs},
    xlabel={Service Level in \%},
    ylabel={Costs in €},
    xmin=0, xmax=100,
    ymin=0, ymax=1000,
    xtick={100,90,80,70,60,50,40,30,20,10,0},
    ytick={100, 200, 300, 400, 500, 600, 700, 800, 900, 1000},
    legend pos=north west,
    ymajorgrids=true,
    grid style=dashed,
]

\addplot[
    color=blue,line width=0.5mm,
    mark=square,
    ]
    coordinates {
    (90.92,909.82)(90,850.60)(80, 712.71)(70, 712.71)(60,666.11)(50,645.72)(40,645.72)(30,645.72)(20,645.72)(10,646.72)(0,645.72)
    };
  
    
\end{axis}
\end{tikzpicture}}
\end{minipage}\hfill

\end{figure}


\end{document}

Is there any way to achieve that?

Best Answer

As mentioned in the comments, empty lines are paragraph breaks, so you've essentially placed the diagrams in three separate paragraphs, and hence three separate lines. This is just the same as normal text, actually. In addition, the total width of the minipages should be less than \textwidth, so change 0.45 to e.g. 0.32.

I disagree with the suggestion to use scalebox, I think it's better to adjust the settings of the diagram. pgfplots has a few predefined styles to make things smaller, they are tiny, footnotesize and small. If you apply the footnotesize style and modify the width you can get the following:

enter image description here

The other modifications are mainly to replace minipage by subfigure and add the optional [t] argument to fix an alignment problem. The misalignment was mostly from breaking the title of the third axis across two lines, because it was too wide. Captions are added in the normal way. (Note that a subfigure environment as defined by the subcaption package is more or less just a minipage, but a \caption added inside it is seen as a caption for a subfigure.)

If you want Figure 1, Figure 2, Figure 3 instead of Figure 1a, 1b, 1c, simply replace subfigure by minipage again.

\documentclass{scrartcl}
\usepackage[margin=0.5in]{geometry}
\usepackage{textcomp}
\usepackage{pgfplots}
\usepackage{subcaption}
\captionsetup{compatibility=false}
\pgfplotsset{width=5cm,compat=1.9}
\begin{document}

\begin{figure}[htbp]
\pgfplotsset{
    footnotesize, % reduces font size and size of diagram
    width=\linewidth, % modify size of axis
}
%Start figure 1
     \begin{subfigure}[t]{0.32\textwidth}
      \centering
            \begin{tikzpicture}
\begin{axis}[
    xlabel={Capacity},
    ylabel={Service Level in \%},
    xmin=0, xmax=8,
    ymin=0, ymax=110,
    xtick={0,1,2,3,4,5,6,7,8},
    ytick={0,20,40,60,80,100},
    legend pos=north west,
    ymajorgrids=true,
    grid style=dashed,
]
\addplot[
    color=blue,line width=0.5mm,
    mark=square,
    ]
    coordinates {
    (2,90.92)(4,97.65)(6,99.94 )(8,100 )(10,100)
    };
\end{axis};
 \end{tikzpicture}
 \caption{foo}
    \end{subfigure}\hfill
     \begin{subfigure}[t]{0.32\textwidth}
     \centering
     \begin{tikzpicture}
\begin{axis}[
    xlabel={Capacity},
    ylabel={Costs in €},
    xmin=0, xmax=8,
    ymin=0, ymax=1000,
    xtick={0,1,2,3,4,5,6,7,8},
    ytick={0,100,200,300,400,500,600,700,800,900,1000},
    legend pos=north west,
    ymajorgrids=true,
    grid style=dashed,
]

\addplot[
    color=blue,line width=0.5mm,
    mark=square,
    ]
    coordinates {
    (2,909.83)(4,740.87)(6,740.87)(8,740.87)(10,740.87)
    };
\end{axis}
\end{tikzpicture}
\caption{bar}
\end{subfigure}\hfill
     \begin{subfigure}[t]{0.32\textwidth}
     \centering
     \begin{tikzpicture}
\begin{axis}[
    title={Sensitivity Analysis\\Service Level vs Costs}, % <-- added \\
    title style={align=center}, % <-- added
    xlabel={Service Level in \%},
    ylabel={Costs in €},
    xmin=0, xmax=100,
    ymin=0, ymax=1000,
    xtick={100,90,80,70,60,50,40,30,20,10,0},
    ytick={100, 200, 300, 400, 500, 600, 700, 800, 900, 1000},
    legend pos=north west,
    ymajorgrids=true,
    grid style=dashed,
]

\addplot[
    color=blue,line width=0.5mm,
    mark=square,
    ]
    coordinates {
    (90.92,909.82)(90,850.60)(80, 712.71)(70, 712.71)(60,666.11)(50,645.72)(40,645.72)(30,645.72)(20,645.72)(10,646.72)(0,645.72)
    };
  
    
\end{axis}
\end{tikzpicture}
\caption{baz}
\end{subfigure}
\caption{foobarbaz}
\end{figure}
\end{document}