1

The following MWE:

\documentclass[a4paper]{article}

\usepackage{subcaption}
\usepackage{pgfplots}
\usepackage{tikz}
\usetikzlibrary{backgrounds, positioning, fit}

\begin{document}

\pgfplotsset{
every axis plot/.append style={line width=0.1pt,draw=black},
every tick label/.append style={font=\tiny},
every x tick label/.append style={font=\tiny},
every y tick label/.append style={font=\tiny}
}

\newcommand{\plotWidth}{4cm}
\newcommand{\plotHeight}{3cm}
\newcommand{\hDist}{0.1\textwidth}
\begin{figure}[htb]
    \centering
    \begin{subfigure}[htb]{\hDist}
        \begin{tikzpicture}
            \begin{axis}[width=\plotWidth,height=\plotHeight,ylabel=$u(x)$,xlabel=$x$,ymin=-1,ymax=1,xmin=-1,xmax=1]
                \addplot[smooth] {x};
            \end{axis}
        \end{tikzpicture}
        %\caption{Subcaption}
    \end{subfigure}
    \hfil
    \begin{subfigure}[htb]{\hDist}
        \begin{tikzpicture}
            \begin{axis}[width=\plotWidth,height=\plotHeight,xlabel=$x$,ymin=-1,ymax=1,xmin=-1,xmax=1]
                \addplot[smooth] {x+x^2};
            \end{axis}
        \end{tikzpicture}
        %\subcaption{Subcaption}
    \end{subfigure} 
    \hfil
    \begin{subfigure}[htb]{\hDist}
        \begin{tikzpicture}
            \begin{axis}[width=\plotWidth,height=\plotHeight,xlabel=$x$,ymin=-1,ymax=1,xmin=-1,xmax=1]
                \addplot[smooth] {x+x^2+x^3};
            \end{axis}
        \end{tikzpicture}
        %\subcaption{Subcaption}
    \end{subfigure}
    \hfil
    \begin{subfigure}[htb]{\hDist}
        \begin{tikzpicture}
            \begin{axis}[width=\plotWidth,height=\plotHeight,xlabel=$x$,ymin=-1,ymax=1,xmin=-1,xmax=1]
                \addplot[smooth] {x+x^2+x^3+x^4};
            \end{axis}
        \end{tikzpicture}
        %\subcaption{Subcaption}
    \end{subfigure}
    \caption{Subcaption}
\end{figure}

\end{document}

produces the following output:

enter image description here

I would like to know how I can align the plots horizontally such that they do not overlap. I tried \hfil, \hfill and \hspace{1cm} to separate the plots which did not work.

  • 1
    Reduce the plotwidth length, and make hDist longer. If you are not going to add subcaptions, have a look at the groupplots library. – Torbjørn T. Jan 23 '19 at 16:25
2

This seems to cry for groupplots.

\documentclass[a4paper]{article}

\usepackage{subcaption}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots} 
%\usetikzlibrary{backgrounds, positioning, fit}

\begin{document}

\pgfplotsset{
every axis plot/.append style={line width=0.1pt,draw=black},
every tick label/.append style={font=\tiny},
every x tick label/.append style={font=\tiny},
every y tick label/.append style={font=\tiny}
}

\newcommand{\plotWidth}{4cm}
\newcommand{\plotHeight}{3cm}
\newcommand{\hDist}{0.1\textwidth}
\begin{figure}[htb]
    \centering
    \begin{tikzpicture}
      \begin{groupplot}[group style={group size=4 by 1},height=\plotHeight,width=\plotWidth]
        \nextgroupplot[ylabel=$u(x)$,xlabel=$x$,ymin=-1,ymax=1,xmin=-1,xmax=1]
           \addplot[smooth] {x};
        \nextgroupplot[xlabel=$x$,ymin=-1,ymax=1,xmin=-1,xmax=1]
            \addplot[smooth] {x+x^2};
        \nextgroupplot[xlabel=$x$,ymin=-1,ymax=1,xmin=-1,xmax=1]
             \addplot[smooth] {x+x^2+x^3};
        \nextgroupplot[xlabel=$x$,ymin=-1,ymax=1,xmin=-1,xmax=1]
                \addplot[smooth] {x+x^2+x^3+x^4};
        \end{groupplot}
      \end{tikzpicture}
        %\subcaption{Subcaption}
    \caption{Subcaption}
\end{figure}
\end{document}

enter image description here

With the current settings the figure is a bit too wide. Options to rectify this include making the individual plots less wide and/or using the setting of the example on p. 456 of the pgfplots manual to use one set of yticks for all plots. Here is a concrete proposal spelling that out.

\documentclass[a4paper]{article}
\usepackage{subcaption}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots} 

\begin{document}

\pgfplotsset{
every axis plot/.append style={line width=0.1pt,draw=black},
every tick label/.append style={font=\tiny},
every x tick label/.append style={font=\tiny},
every y tick label/.append style={font=\tiny}
}

\newcommand{\plotWidth}{4cm}
\newcommand{\plotHeight}{3cm}
\newcommand{\hDist}{0.03\textwidth}
\begin{figure}[htb]
    \centering
    \begin{tikzpicture}
      \begin{groupplot}[group style={group size=4 by 1, y descriptions at=edge
      left, horizontal sep=\hDist},height=\plotHeight,width=\plotWidth,
      xlabel=$x$,ymin=-1,ymax=1,xmin=-1,xmax=1]
        \nextgroupplot[ylabel=$u(x)$]
           \addplot[smooth] {x};
        \nextgroupplot[]
            \addplot[smooth] {x+x^2};
        \nextgroupplot[]
             \addplot[smooth] {x+x^2+x^3};
        \nextgroupplot[]
                \addplot[smooth] {x+x^2+x^3+x^4};
        \end{groupplot}
      \end{tikzpicture}
    \caption{A group of plots.}
\end{figure}
\end{document}

enter image description here

I do not know whether one can combine "real" captions with group plots. What one can certainly do is to use the title as a replacement of a caption.

\documentclass[a4paper]{article}
\usepackage{subcaption}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots} 

\begin{document}

\pgfplotsset{
every axis plot/.append style={line width=0.1pt,draw=black},
every tick label/.append style={font=\tiny},
every x tick label/.append style={font=\tiny},
every y tick label/.append style={font=\tiny},
every axis title/.append style={at={(0.5,-1.1)}}}
\newcommand{\plotWidth}{4cm}
\newcommand{\plotHeight}{3cm}
\newcommand{\hDist}{0.03\textwidth}
\begin{figure}[htb]
    \centering
    \begin{tikzpicture}
      \begin{groupplot}[group style={group size=4 by 1, y descriptions at=edge
      left, horizontal sep=\hDist},height=\plotHeight,width=\plotWidth,
      xlabel=$x$,ymin=-1,ymax=1,xmin=-1,xmax=1]
        \nextgroupplot[ylabel=$u(x)$,title={Left.}]
           \addplot[smooth] {x};
        \nextgroupplot[title={Rather left.}]
            \addplot[smooth] {x+x^2};
        \nextgroupplot[title={Rather right.}]
             \addplot[smooth] {x+x^2+x^3};
        \nextgroupplot[title={Right.}]
                \addplot[smooth] {x+x^2+x^3+x^4};
        \end{groupplot}
      \end{tikzpicture}
    \caption{A group of plots.}
\end{figure}
\end{document}

enter image description here

|improve this answer|||||
  • Is there a simple way to add subcaptions to each plot? – random9 Jan 23 '19 at 23:36
  • @Samuel I have never tried to add a "real" subcaption. You can definitely move the title below the figure. I added an example. Whether or or not one can use a subcaption would IMHO deserve a new question. – user121799 Jan 24 '19 at 0:10

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.