4

I have the following MWE

\documentclass[preview]{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{graphicx, float}
\usepackage{subcaption}

\def\figone{%
    \begin{tikzpicture}
        \begin{axis}[%
             width = 1.2\textwidth,
             height = \textwidth,
             grid = both,
             xtick distance = 1,
             ymin = 0,
             ymax = 2,
             ytick distance = 0.4,
             xlabel = $t/\tau$,
             ylabel = $V/V_0$
            ]
            \addplot[red, ultra thick]{exp(x)};
        \end{axis}
    \end{tikzpicture}
    }

\def\figtwo{%
    \begin{tikzpicture}
        \begin{axis}[%
             width = 1.2\textwidth,
             height = \textwidth,
             grid = both,
             xtick distance = 1,
             ymin = 0,
             ymax = 2,
             ytick distance = 0.4,
             yticklabels={,,},
             xlabel = $t/\tau$
            ]
            \addplot[blue, ultra thick]{exp(2*x)};
        \end{axis}
    \end{tikzpicture}
    }


\begin{document}

\begin{figure}[ht]
    \centering
    \begin{subfigure}[t]{0.45\textwidth}
        \figone
    \end{subfigure}
    \hspace{5.5mm}
    \begin{subfigure}[t]{0.45\textwidth}
        \figtwo
    \end{subfigure}
    \caption{Caption}
    \label{fig:my_label}
\end{figure}





\end{document}

enter image description here

Observe that I used \hspace{5.5mm} to gather the graphs. I got such value of 5.5mm by trial and error. If I comment that line, the graphs will merge such that the value 0 of the left plot will coincide with the edge of the right one.

My question is: Is there any way to make the plots automatically glue together side by side?

5
  • 1
    The MWE does not compile.
    – Bibi
    Commented Jun 3, 2019 at 14:08
  • Strange... it compiles for me. I will try changing documentclass to article... if it works, I will edit the post.
    – Brasil
    Commented Jun 3, 2019 at 14:14
  • Works fine for me.
    – Brasil
    Commented Jun 3, 2019 at 14:20
  • I added a picture to show the result.
    – Brasil
    Commented Jun 3, 2019 at 14:24
  • Strange indeed (I get dimension too large errors...). Anyway, I think you will find what your are looking for in pgfplots manual, section 4.19 (Alignment Options), p.369.
    – Bibi
    Commented Jun 3, 2019 at 16:28

1 Answer 1

7

I think the easiest way to achieve what you want is to use PGFPlots library when you don't need to separate images.

% used PGFPlots v1.16
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    \usepgfplotslibrary{groupplots}
    \pgfplotsset{compat=1.3}
\begin{document}
\begin{tikzpicture}
    \begin{groupplot}[
        group style={
            group size=2 by 1,
            horizontal sep=0pt,
            % show `ylabel` and `yticklabels` only on the left (most) plot
            y descriptions at=edge left,
        },
        width=0.45\textwidth,
        height=0.45\textwidth,
        grid=both,
        ymin=0,
        ymax=2,
        xtick distance=1,
        ytick distance=0.4,
        xlabel=$t/\tau$,
        ylabel=$V/V_0$,
    ]
    \nextgroupplot
        \addplot [red,ultra thick] {exp(x)};
    \nextgroupplot
        \addplot [blue,ultra thick] {exp(0.5*x)};
    \end{groupplot}
\end{tikzpicture}
\end{document}

image showing the result of above code

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .