0

I'm new latex. I've been trying to learn how to format these plots side by side on one line, but I have not been able to successfully. I don't understand how to use the \begin{figure}[h] feature or the subfigure. Any help would be appreciated.

\documentclass[a4paper,12pt,twoside]{book}
\usepackage{amsmath,bm, graphicx, pgfplots}
\usepackage{pgfplots}
\pgfplotsset{compat=1.7}
\usepackage{graphicx}
\graphicspath{ {Figure_1.png}, {logo.gif} }
\usepackage{amsmath}
\usepackage{nccmath}
\usepackage[english]{babel}
\usepackage{blindtext}
\usepackage[utf8]{inputenc}
\usepackage{blindtext}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{hyphenat}
\usepackage{graphicx}
\usepackage{subfigure}
\usepackage{subfig}
\usepackage{multirow}
\usepackage{array}
\usepackage{graphicx,subfigure}



\begin{document}
\begin{figure}[h]
\centering 
    \subfigure[Figure A]{\label{fig:a}
         \begin{tikzpicture}[baseline=0pt]
        \begin{axis}[
            xmin=-2.5, xmax=2.5,
            ymin=-1.5, ymax=1.5,
            axis lines=center,
            axis on top=true,
            domain=-2.5:2.5,
            ylabel=$y$,
            xlabel=$x$,
            ]
        \\
            \addplot [mark=none,draw=red,ultra thick] {0.5*(1 +tanh(\x))};
            \node [right, red] at (axis cs: -1.1,1.1) {$y = 0.5 *(1 + \tanh x)$};
            
            %% Add the asymptotes
            \draw [blue, dotted, thick] (axis cs:-2.5,0)-- (axis cs:2.5,0);
            \draw [blue, dotted, thick] (axis cs:-2.5,+1)-- (axis cs:2.5,+1);
        \end{axis}
        \end{tikzpicture} 
    }
    \subfigure[Figure B]{\label{fig:b}
      \begin{tikzpicture}[
          declare function={
            func(\x)= (\x <= 0) * (0)   +
                      and(\x > 0 , \x < 1) * (\x)     +
                      (\x >= 1) * (1)
           ;
          }
        ]
        \begin{axis}[
            xmin=-2.5, xmax=2.5,
            ymin=-1.5, ymax=1.5,
            axis lines=center,
            axis on top=true,
            domain=-2.5:2.5,
            ylabel=$y$,
            xlabel=$x$,
        ]
        
        \addplot [blue,thick] {func(\x)};
        \end{axis}
        \end{tikzpicture} 
   }
    \subfigure[Figure B]{\label{fig:c}
     \begin{tikzpicture}[
          declare function={
            gunc(\x)= (\x <= 0) * (0)   +
                      (\x > 0) * (\x)     
           ;
          }
        ]
        \begin{axis}[
            xmin=-2.5, xmax=2.5,
            ymin=-1.5, ymax=1.5,
            axis lines=center,
            axis on top=true,
            domain=-2.5:2.5,
            ylabel=$y$,
            xlabel=$x$,
        ]
        
        \addplot [cyan,thick] {gunc(\x)};
        \end{axis}
        \end{tikzpicture} 
    \label{fig:image3}
    }
\end{figure}
\end{document}
0
1

To use the subfigure environments inside a figure environment you need to load up the caption and subcaption packages.

To be able to make them fit in one line they were scaled to 0.6.

Note You must clean your preamble. Many packages were loaded two or three times.

b

\documentclass[a4paper,12pt,twoside]{book}

\usepackage{pgfplots}
\pgfplotsset{compat=1.7}
\usepackage{amsmath}

\usepackage{caption} % added <<<<<<
\usepackage{subcaption}% added <<<<<<

\usepackage{showframe}%show margins <<<

\begin{document}
    \begin{figure}[h]
        \centering 
\begin{subfigure}[b]{0.3\textwidth}
            \begin{tikzpicture}[baseline=0pt, scale=0.6,]
                \begin{axis}[
                    xmin=-2.5, xmax=2.5,
                    ymin=-1.5, ymax=1.5,
                    axis lines=center,
                    axis on top=true,
                    domain=-2.5:2.5,
                    ylabel=$y$,
                    xlabel=$x$,
                    ]
                    \addplot [mark=none,draw=red,ultra thick] {0.5*(1 +tanh(\x))};
                    \node [right, red] at (axis cs: -1.1,1.1) {$y = 0.5 *(1 + \tanh x)$};                   
                    %% Add the asymptotes
                    \draw [blue, dotted, thick] (axis cs:-2.5,0)-- (axis cs:2.5,0);
                    \draw [blue, dotted, thick] (axis cs:-2.5,+1)-- (axis cs:2.5,+1);
                \end{axis}
            \end{tikzpicture}
            \caption{one}
            \label{fig:a}
    \end{subfigure}
\hfill
\begin{subfigure}[b]{0.3\textwidth}
            \begin{tikzpicture}[scale=0.6,
                declare function={
                    func(\x)= (\x <= 0) * (0)   +
                    and(\x > 0 , \x < 1) * (\x)     +
                    (\x >= 1) * (1)
                    ;           
                }
                ]
                \begin{axis}[
                    xmin=-2.5, xmax=2.5,
                    ymin=-1.5, ymax=1.5,
                    axis lines=center,
                    axis on top=true,
                    domain=-2.5:2.5,
                    ylabel=$y$,
                    xlabel=$x$,
                    ]                   
                    \addplot [blue,thick] {func(\x)};
                \end{axis}
            \end{tikzpicture}
            \caption{two}
             \label{fig:b}
    \end{subfigure} 
\hfill 
\begin{subfigure}[b]{0.3\textwidth}
            \begin{tikzpicture}[scale=0.6,
                declare function={
                    gunc(\x)= (\x <= 0) * (0)   +
                    (\x > 0) * (\x)     
                    ;
                }
                ]
                \begin{axis}[
                    xmin=-2.5, xmax=2.5,
                    ymin=-1.5, ymax=1.5,
                    axis lines=center,
                    axis on top=true,
                    domain=-2.5:2.5,
                    ylabel=$y$,
                    xlabel=$x$,
                    ]                       
                    \addplot [cyan,thick] {gunc(\x)};
                \end{axis}
            \end{tikzpicture}
            \caption{three}
             \label{fig:c}
    \end{subfigure} 
    \caption{Three graphs}
        \label{fig:image3}
    \end{figure}
\end{document}
5
  • How do you learn how to use this package? Jul 7 at 18:35
  • 1
    @Robert Csete Google latex subfigures and you will find a good tutorial in https://www.overleaf.com/learn/latex/How_to_Write_a_Thesis_in_LaTeX_(Part_3):_Figures,_Subfigures_and_Tables . (and many others). If everything fails, read the manuals. Perhaps you are lucky and they will have examples. Jul 7 at 18:42
  • Uh, so ancient pgfplots version :-(. I believe that is time to upgrade (now is actual 1.18) :-)
    – Zarko
    Jul 7 at 18:57
  • @Zarko what's the new version I'm trying to use this code in a document and I keep getting an error:missing number treated as zero subfigure Jul 7 at 19:05
  • 1
    @RobertCsete, pgfplots has nothing with subcaption. it only enables easier and better way to draw diagrams. Testing above answer with \pgfplotsset{compat=1.18} instead of \pgfplotsset{compat=1.7}, not produce any warnings nor errors.
    – Zarko
    Jul 7 at 19:11
0

As supplement to @Simon Dispa answer, With use of recent version of the pgfplots package (if you not have installed, it is time now to do this), the code is a bit shorter.

Since all images have the same axis options, they can be merged to common settings for all diagrams in the \pgfplotsset inserted in figure:

\documentclass[a4paper,12pt,twoside]{book}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18} % <----

\usepackage{caption} 
\usepackage{subcaption}

%--------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%

\begin{document}
    \begin{figure}[ht]
    \centering
    \pgfplotsset{
        xmin=-2.5, xmax=2.5,
        ymin=-1.5, ymax=1.5,
        axis lines=center,
        axis on top=true,
        domain=-2.5:2.5,
        ylabel=$y$,
        xlabel=$x$,
                }
    
\begin{subfigure}[b]{0.3\textwidth}
        \begin{tikzpicture}[scale=0.6,]
    \begin{axis}
\addplot [mark=none,draw=red,ultra thick] {0.5*(1 +tanh(\x))};
\node [right, red] at (-1.1,1.1) {$y = 0.5 *(1 + \tanh x)$};
%% Add the asymptotes
\draw [blue, dotted, thick] (-2.5,0) -- (2.5,0);
\draw [blue, dotted, thick] (-2.5,1) -- (2.5,1);
    \end{axis}
        \end{tikzpicture}
    \caption{one}
    \label{fig:a}
    \end{subfigure}%
\hfill%
\begin{subfigure}[b]{0.3\textwidth}
        \begin{tikzpicture}[scale=0.6,
    declare function={func(\x)= (\x <=0) * (0)  +
                                and(\x >0, \x < 1) * (\x) +
                                (\x >=1) * (1);}
                            ]
    \begin{axis}
\addplot [blue,thick] {func(\x)};
    \end{axis}
        \end{tikzpicture}
    \caption{two}
    \label{fig:b}
\end{subfigure}%
\hfill%
\begin{subfigure}[b]{0.3\textwidth}
        \begin{tikzpicture}[scale=0.6,
    declare function={gunc(\x)= (\x <=0) * (0) +
                                (\x > 0) * (\x);}
                            ]
    \begin{axis}
\addplot [cyan,thick] {gunc(\x)};
    \end{axis}
    \end{tikzpicture}
\caption{three}
\label{fig:c}
\end{subfigure}

\caption{Three graphs}
\label{fig:image3}
    \end{figure}
\end{document}

enter image description here

(red lines indicate page layout)

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.