how to align two tikzpicture plots in latex

18,158

There are two problems with your code.

First horizontal alignment of the figures was incorrect, but this can easily be fixed by using

\begin{subfigure}[b]{0.3\textwidth}

instead of

\begin{subfigure}[b!]{0.3\textwidth}

concerning the width, what is done when you create a subfigure environment is that a minipage of the indicated width is created. But is is up to you to respect this width with your content, no rescaling is done.

For instance, if, in a subfigure, you include an image and give it a width of \linewidth, the width will be respected. But if you give this image a 15cm width, probably it will be larger than you minipage. But LaTeX will respect your directives (and indicate an overfull hbox).

This is the problem that you have. Your plots are too large and overlap.

There are two ways to fix that.

  • You can give a width=\linewidth parameter to the axis environment, but it generally requires a redesign of your plot

  • you can rescale the box created by tikz. The most flexible way to do that is with adjustbox package

\documentclass{article}

\usepackage{subcaption}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{adjustbox}

\begin{document}

\begin{figure}
  \centering
  \begin{subfigure}[b]{0.45\textwidth}
%%%    \begin{adjustbox}{width=\linewidth} % rescale box
    \begin{tikzpicture}
      \begin{axis}[
%%%        width=\linewidth,            % or modify the plot width
        axis y line = middle,
        ...
        ...
      \end{axis}
    \end{tikzpicture}
%%%  \end{adjustbox}       %
\end{subfigure}%
   \hfill
  \begin{subfigure}[b]{0.45\textwidth}
etc.

Adding a width parameter to the axis environment

enter image description here

Rescaling with adjustbox

enter image description here

BTW, if you do not intend to add subcaptions to your plots, the subfigure environment is useless, and you can just put your (properly scaled) tikzpictures side by side separated by an \hfill.

Share:
18,158
someone serious
Author by

someone serious

Updated on June 19, 2022

Comments

  • someone serious
    someone serious almost 2 years

    I am trying to use the subfigure method shown at https://www.latex-tutorial.com/tutorials/figures/ to make side by side plots but I can't seem to adjust the size and get them to go side by side...what am i doing wrong? Below is code I am using

    \begin{figure}
            \centering
            \begin{subfigure}[b!]{0.3\textwidth}
                \begin{tikzpicture}
                    \begin{axis}[
                        axis y line = middle,
                        axis x line = middle,
                        xlabel = $x$,
                        ylabel = {$f(x) = x^3$},
                        grid=major,
                    ]
                    \addplot [
                        domain=-3:3, 
                        samples=100, 
                        color=red,
                    ]
                    {x^3};
                    \addlegendentry{$x^3$}
                    %
                    \addplot [
                        domain=-3:3, 
                        samples=100, 
                        color=blue,
                        ]
                        {x^3 + 3};
                    \addlegendentry{$x^3 + 3$}
                     %
                    \addplot [
                        domain=-3:3, 
                        samples=100, 
                        color=green,
                        ]
                        {x^3 - 3};
                    \addlegendentry{$x^3 - 3$}
                    \end{axis}
                \end{tikzpicture}
            \end{subfigure}
            %\hfill
            \begin{subfigure}[b]{0.3\textwidth}
                \begin{tikzpicture}
                    \begin{axis}[
                        axis y line = middle,
                        axis x line = middle,
                        xlabel = $x$,
                        ylabel = {$f(x) = x^3$},
                        grid=major,
                    ]
                    \addplot [
                        domain=-3:3, 
                        samples=100, 
                        color=red,
                    ]
                    {x^3};
                    \addlegendentry{$x^3$}
                    \end{axis}
                \end{tikzpicture}
            \end{subfigure}
            \caption{lajsdfls}
        \end{figure}
    
  • someone serious
    someone serious over 4 years
    thank you, would you mind explaining a little more how the whole 0.4\linewidth works for a subfigure but is different from in the axis and all that? I just started using latex yesterday so i'm p lost. thank you.
  • someone serious
    someone serious over 4 years
    i did use adjust box though and it works, now i am trying to get the entire figure to the top of the page...i got it to top with \begin{figure}[H]