I have two plots I would like to put in a grouppolt environment with a unique legend. Each plot is generated by a given set of data using the command \pgfplotstableread{directory}{\DataTable}.

Now let's imagine we have the following code structure for each plot:

        title = Time evolution of mass,
        xmin = 0, xmax = 42,
        ymin = 0.3, ymax = 1.05,
        xtick distance = 5,
        ytick distance = 0.2,
        grid = both,
        grid style = {dotted},
        minor tick num = 1,
        major grid style = {lightgray!75},
        minor grid style = {lightgray!75},
        width = 0.85\textwidth,
        height = 0.50\textwidth,
        %scale only axis,
        legend cell align = {left},
        legend pos = north east
    \addplot[smooth, black, ultra thick] table [x = {t1a}, y = {m1a}] {\DataTable};
    \addplot[smooth, Lavender, very thick] table [x = {t1b}, y = {m1b}] {\DataTable};
    \addplot[smooth, Violet, very thick] table [x = {t1c}, y = {m1c}] {\DataTable};
    \addplot[smooth, Emerald, dotted, ultra thick] table [x = {t2}, y = {m2}] {\DataTable};

    n1 (k),
    n1 (k-2),
    n1 (k-4),

    \draw[-{Circle[open, width=4pt, length=4pt]}, shorten >=-2pt,shorten <=0mm] (rel axis cs:0.5,0.31) node[left,draw=Emerald, line width=0.20mm, fill=Emerald!10, align=left] {$m_f=0.530$}  -- (axis cs:26.742, 0.530);

    \draw[-{Circle[open, width=4pt, length=4pt]}, shorten >=-2pt,shorten <=0mm] (rel axis cs:0.83,0.125) node[left,draw=Lavender, line width=0.20mm, fill=Lavender!10, align=left] {$m_f=0.528$}  -- (axis cs:27.887, 0.528); %add open, before width option inside Circle to have a not filled circle

    \draw[-{Circle[open, width=4pt, length=4pt]}, shorten >=-2pt,shorten <=0mm] (rel axis cs:0.76,0.3) node[right,draw=Violet, line width=0.20mm, fill=Violet!10, align=left] {$m_f=0.525$}  -- (axis cs:29.005, 0.525);

\caption{Mass $m$ as a function of time}

I would like that the two plots should be positioned like 1 column and 2 rows, i.e. you should be able to read them downward, in addition they should have the same legend, positioned centered below, and they should keep their own heading. Here an example:

enter image description here

I'm not able to provide a MWE, because the data set is quite big. I just need the basic structure for this kind of group style, and then I would adapt my code to that structure. I hope someone can help me.

  • It probably doesn't matter where the data comes from, so for an MWE you might be able to use just multiple \addplot {rnd};. Alternatively, read in a table with two or three rows. Jun 19, 2021 at 16:54

1 Answer 1


Like this:?

enter image description here

By use of the pgfplots.groupplots library:


        width=\textwidth, height=75mm,
        grid = both,
        grid style = {dotted},
        minor tick num = 3,
        major grid style = {lightgray!75},
        minor grid style = {lightgray!25},
        legend style={at={(0.5,-0.2)},
                      anchor=north,legend columns=-1},
        legend cell align = {left},
Iter    Abs1        Abs2        x           y
 1,     0.9317521,  1.1849326,  1.6130556,  0.8022207
 2,     1.8946202,  1.1228282,  1.8964566, -0.5353802
 3,     1.5243302,  1.0372991,  1.4375012,  0.9719003
 4,     1.5797030,  1.1346832,  1.8717142,  0.3138737
 5,     1.8814457,  1.0529187,  2.0568468, -0.5509391
 6,     2.0435003,  1.0470546,  2.0621956, -0.3565483
 7,     2.0373926,  1.1215579,  2.1836100,  0.3360301
 8,     1.9797077,  1.1632352,  1.8299063,  0.3871091
 9,     1.9972528,  1.1952478,  1.8133509, -0.0867033
10,     1.8320176,  1.0625633,  1.0727495,  1.7256738

\begin{groupplot}[group style={
                  group size=1 by 2,
                  vertical sep=6em,
                  ylabels at=edge left}]
% left diagram
\nextgroupplot[title = {\emph{Heading of the top image}},
               ylabel= {c/mol/L},
               xlabel= {time $t/h$},
               ]{%Graph column 1 versus column 0
\addplot table[x index=0,y index=1,col sep=comma] {data.csv};
\addplot table[x index=0,y index=2,col sep=comma] {data.csv};
% right diagram
\nextgroupplot[title = {\emph{Heading of the bottom image}},
               ylabel= {c/mol/L},
               xlabel= {time $t/h$},
               ]{%Graph column 2 versus column 0
\addplot table[x index=0,y index=3,col sep=comma] {data.csv};
\addplot table[x index=0,y index=4,col sep=comma] {data.csv};
    \legend{legend, legend}
    \caption{Mass $m$ as a function of time}

Data table is borrowed from some my old answer.

  • Thank you very much @Zarko, that's exactly what I needed.
    – Catarella
    Jun 20, 2021 at 8:29

You must log in to answer this question.

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