1

So I'm using Overleaf and just made a line graph with nine different lines in it. It looks like this:

1

However, I want each line to have a separate graph but the graphs should all be grouped together, so in the end, it looks something like this (but instead of 3,3 it should be 3,3,3):

2

Anyone knows how to solve this? Here is my code:

    \documentclass[12pt]{article}
    \usepackage{pgfplots}
    \usepackage{pgfplotstable}
    \usepackage{tikz}
    \begin{document}
    
    \begin{figure}[!htb]
        \centering
    \caption{Trait Attacks in the Swedish Multi-Party System 1970-2022.}
    \begin{tikzpicture}
    \pgfplotsset{scale only axis,}
    \begin{axis}[x tick label style={/pgf/number format/.cd,%
            scaled x ticks = false,
            set thousands separator={},
            fixed}, yticklabel={$\pgfmathprintnumber{\tick}\%$}, grid=both, minor tick num=9, grid style={line width=.2pt, draw=black!10}, major grid style={line width=.4pt, draw=black!20}, enlargelimits={abs=0.1}, width=12cm, height=8cm, xmin=1970, xmax=2022, ymin=0, ymax=30, xlabel= \textbf{Time}, ylabel= \textbf{Degree of Trait Attacks}, xtick {1970,1973,1976,1979,1982,1985,1988,1991,1994,1998,2002,2006,2010,2014,2018,2022}, every tick label/.append style={font=\tiny}]
    \addplot[color=purple,,mark=*]
    coordinates{ 
    (1970,9.803922)
    (1973,3.703704)
    (1976,3.738318)
    (1979,3.726708)
    (1982,5.882353)
    (1985,6.4516129)
    (1988,1.5463918)
    (1991,2.419355)
    (1994,3.7593985)
    (1998,1.538462)
    (2002,2.272727)
    (2006,3.703704)
    (2010,0)
    (2014,0) 
    (2018,0) 
    (2022,0)};
    \addplot[color=red,,mark=*]
    coordinates{ 
    (1970,9.489051)
    (1973,3.703704)
    (1976,6.666667)
    (1979,9.183673)
    (1982,25)
    (1985,6.849315)
    (1988,3.703704)
    (1991,4.4247788)
    (1994,3.149606)
    (1998,3.703704)
    (2002,3.703704)
    (2006,3.703704)
    (2010,0)
    (2014,0) 
    (2018,0) 
    (2022,0)};
    \addplot[color=lime,,mark=*]
    coordinates{ 
    (1988,3.703704)
    (1991,0)
    (1994,3.703704)
    (1998,1.234568)
    (2002,3.703704)
    (2006,3.703704)
    (2010,0)
    (2014,0) 
    (2018,0) 
    (2022,0)};
    \addplot[color=green,,mark=*]
    coordinates{ 
    (1970,3.703704)
    (1973,3.703704)
    (1976,1.923077)
    (1979,3.2942104)
    (1982,3.703704)
    (1985,3.703704)
    (1988,3.703704)
    (1991,3.448276)
    (1994,3.703704)
    (1998,3.703704)
    (2002,4.5112782)
    (2006,3.703704)
    (2010,0)
    (2014,0) 
    (2018,0) 
    (2022,0)};
    \addplot[color=blue,,mark=*]
    coordinates{ 
    (1970,3.703704)
    (1973,3.703704)
    (1976,3.703704)
    (1979,3.703704)
    (1982,3.703704)
    (1985,4.8275862)
    (1988,3.703704)
    (1991,3.333333)
    (1994,5)
    (1998,4.242424)
    (2002,2.1097046)
    (2006,4.3010753)
    (2010,0)
    (2014,0)
    (2018,0)  
    (2022,0)};
    \addplot[color=teal,mark=*]
    coordinates{ 
    (1991,3.703704)
    (1994,3.703704)
    (1998,1.020408)
    (2002,2.139037)
    (2006,1.8348624)
    (2010,0)
    (2014,0)
    (2018,0)
    (2022,0)};
    \addplot[color=brown,,mark=*]
    coordinates{ 
    (2010,0)
    (2014,0)
    (2018,0)
    (2022,0)};
    \addplot[color=cyan,,mark=*]
    coordinates{ 
    (1970,9.756098)
    (1973,2.564103)
    (1976,2)
    (1979,5.333333)
    (1982,6.25)
    (1985,6.593407)
    (1988,3.703704)
    (1991,0.621118)
    (1994,1.298701)
    (1998,3.703704)
    (2002,0.7272727)
    (2006,3.703704)
    (2010,0)
    (2014,0) 
    (2018,0) 
    (2022,0)};
    \addplot[color=yellow,,mark=*]
    coordinates{ 
    (1991,2.1390374)
    (1994,0)};
    \legend{V, S, MP, C, L, KD, SD, M, NyD}
    \end{axis}
    \end{tikzpicture}
    \end{figure}
    \end{document}

1 Answers1

1

Try to use subfigures from the subcaption package...
I used different tikz pictures from an other answer of mine..

\documentclass[12pt]{article}
    \usepackage{pgfplots}
    \usepackage{pgfplotstable}
    \usepackage{subcaption}
    \usepackage{tikz}
    \begin{document}
    
    \begin{figure}[!htb]
        \centering
           \begin{subfigure}[b]{0.3\textwidth}
             \centering
             \caption{country A}
             \begin{tikzpicture}
                %% vertices
                \draw[fill=black] (0,0) circle (1.5pt);
                \draw[fill=black] (3/2,0) circle (1.5pt);
                \draw[fill=black] (0.75,1.2) circle (1.5pt);
                %% vertex labels
                \node at (-0.2,-0.3) {$v_{3}$};
                \node at (1.7,-0.3) {$v_{2}$};
                \node at (0.75,1.5) {$v_{1}$};
                %%% edges
                \draw[thick] (0,0) -- (3/2,0) -- (0.75,1.2);
                \end{tikzpicture}
             \label{subfig:countryA}
         \end{subfigure}
         \hfill
         \begin{subfigure}[b]{0.3\textwidth}
             \centering
             \caption{country B}
            \begin{tikzpicture}
                %% vertices
                \draw[fill=black] (0,0) circle (1.5pt);
                \draw[fill=black] (3/2,0) circle (1.5pt);
                \draw[fill=black] (0.75,1.2) circle (1.5pt);
                %% vertex labels
                \node at (-0.2,-0.3) {$v_{3}$};
                \node at (1.7,-0.3) {$v_{2}$};
                \node at (0.75,1.5) {$v_{1}$};
                %%% edges
                \draw[thick] (0,0) -- (3/2,0) -- (0.75,1.2);
                \end{tikzpicture}
             \label{subfig:countryB}
         \end{subfigure}
         \hfill
         \begin{subfigure}[b]{0.3\textwidth}
             \centering
             \caption{country C}
             \begin{tikzpicture}
            %% vertices
            \draw[fill=black] (0,0) circle (1.5pt);
            \draw[fill=black] (3/2,0) circle (1.5pt);
            \draw[fill=black] (0.75,1.2) circle (1.5pt);
            %% vertex labels
            \node at (-0.2,-0.3) {$v_{3}$};
            \node at (1.7,-0.3) {$v_{2}$};
            \node at (0.75,1.5) {$v_{1}$};
            %%% edges
            \draw[thick] (0,0) -- (3/2,0) -- (0.75,1.2);
            \end{tikzpicture}
             \label{subfig:countryC}
         \end{subfigure}
         
              \begin{subfigure}[b]{0.3\textwidth}
             \centering
             \caption{country D}
             \begin{tikzpicture}
                %% vertices
                \draw[fill=black] (0,0) circle (1.5pt);
                \draw[fill=black] (3/2,0) circle (1.5pt);
                \draw[fill=black] (0.75,1.2) circle (1.5pt);
                %% vertex labels
                \node at (-0.2,-0.3) {$v_{3}$};
                \node at (1.7,-0.3) {$v_{2}$};
                \node at (0.75,1.5) {$v_{1}$};
                %%% edges
                \draw[thick] (0,0) -- (3/2,0) -- (0.75,1.2);
                \end{tikzpicture}
             \label{fsubfig:countryD}
         \end{subfigure}
         \hfill
         \begin{subfigure}[b]{0.3\textwidth}
             \centering
             \caption{country E}
            \begin{tikzpicture}
                %% vertices
                \draw[fill=black] (0,0) circle (1.5pt);
                \draw[fill=black] (3/2,0) circle (1.5pt);
                \draw[fill=black] (0.75,1.2) circle (1.5pt);
                %% vertex labels
                \node at (-0.2,-0.3) {$v_{3}$};
                \node at (1.7,-0.3) {$v_{2}$};
                \node at (0.75,1.5) {$v_{1}$};
                %%% edges
                \draw[thick] (0,0) -- (3/2,0) -- (0.75,1.2);
                \end{tikzpicture}
             \label{subfig:countryE}
         \end{subfigure}
         \hfill
         \begin{subfigure}[b]{0.3\textwidth}
             \centering
             \caption{country F}
             \begin{tikzpicture}
            %% vertices
            \draw[fill=black] (0,0) circle (1.5pt);
            \draw[fill=black] (3/2,0) circle (1.5pt);
            \draw[fill=black] (0.75,1.2) circle (1.5pt);
            %% vertex labels
            \node at (-0.2,-0.3) {$v_{3}$};
            \node at (1.7,-0.3) {$v_{2}$};
            \node at (0.75,1.5) {$v_{1}$};
            %%% edges
            \draw[thick] (0,0) -- (3/2,0) -- (0.75,1.2);
            \end{tikzpicture}
             
             \label{subfig:countryF}
         \end{subfigure}
         \caption{Trait Attacks in the Swedish Multi-Party System 1970-2022.}
        \label{fig:six_graphs}

    \end{figure}
    \end{document}

enter image description here

{0.3\textwidth} for every subfigure achieves a figure width that triggers a line break after three subfigures in one row. By this you can easily add a third row in \begin{figure}...\end{figure}for a 3x3 graphic.

sebo1234
  • 608
  • 5
  • 18