2

I'm making a figure with three subfigures inside of it. I'd then like to create a legend on the top left of the figure. Currently I've created the legend in the third subfigure but it would be ideal to have it top left of the figure itself.

Thanks for any help you can provide on this :)

\begin{figure}[tbh]{
 \subfigure[]{         
  \begin{tikzpicture}
    \pgfplotsset{width = .3\textwidth, compat = newest}
    \begin{axis}[...]
        \addplot [...] file{...};
        \addplot [...] file{...};
    \end{axis}
\end{tikzpicture}}
\subfigure[]{         
  \begin{tikzpicture}
    \pgfplotsset{width = .3\textwidth, compat = newest}
    \begin{axis}[...]
        \addplot [...] file{...};
        \addplot [...] file{...};
    \end{axis}
\end{tikzpicture}}
\subfigure[]{         
  \begin{tikzpicture}
    \pgfplotsset{width = .3\textwidth, compat = newest}
    \begin{axis}[...]
        \addplot [...] file{...}; \addlengentry{a}
        \addplot [...] file{...}; \addlengentry{b} 
    \end{axis}
   \end{tikzpicture}}   
  \caption{...}
  \label{...}
  }
\end{figure}$
Andrew Swann
  • 95,762

2 Answers2

1

As Andrew has already mentioned in his comment to your question one solution would be to use the groupplots library of pgfplots, which I also present in my solution. But this approach can be adapted to three normal axis environments next to each other as well.

Also for that you simply add some dummy lines to the first plot so all the entries you want to show in the legend are there with the same style and then place the legend relative to the plot on top of it.

For more details have a look at the comments in the code.

\documentclass[border=2mm]{standalone}
\usepackage{pgfplots}
    \usepgfplotslibrary{
        groupplots,
    }
    \pgfplotsset{
        compat=1.13,
        % define a style and put stuff in there which all plots have in common
        my style/.style={
            % reduce the `width' and `height' of the plots
            width=4cm,height=4cm,
            % set the labels for the axis
            xlabel=$x$,
            ylabel=$y$,
        },
        % define another style which contains the stuff of the legend
        my legend style/.style={
            legend entries={
                blue plot,
                red plot,
                green plot
            },
            legend style={
                at={([yshift=2pt]0,1)},
                anchor=south west,
            },
            legend columns=4,
        },
        % define a `cycle list` which will be used in the plots
        cycle multiindex* list={
            blue!75!black,
            red!75!black,
            green!75!black
                \nextlist
            mark=*,
            mark=square*,
            mark=triangle*
                \nextlist
        },
    }
\begin{document}
\begin{tikzpicture}
    % if the plots are very similar, use the `groupplots' axis environment
    \begin{groupplot}[
        group style={
            % place the three plots next to each other
            group size=3 by 1,
            % only set the tick and axis labels at the plots on the left
            % and on the bottom
            x descriptions at=edge bottom,
            y descriptions at=edge left,
        },
        % load the style created in the preamble
        my style,
        %
        % define the axis limits for the plots ...
        xmin=0,xmax=2,
        ymin=0,ymax=2,
        % ... and enlarge them a bit (use the default value)
        enlargelimits=true,
    ]
        \nextgroupplot[
            my legend style,
        ]
            \addplot coordinates {(0,0) (1,1) (2,2)};
            % to one of the plots you have to add some dummy plots
            % so they are collected/stored in the legend
            \addplot coordinates {(-1,-1)};
            \addplot coordinates {(-1,-1)};
        \nextgroupplot[
            % start with the second item of the `cycle list' here, so
            % shift it one element
            cycle list shift=1,
        ]
            \addplot coordinates {(0,2) (1,1) (2,0)};
        \nextgroupplot[
            % start with the third item of the `cycle list' here, so
            % shift it two elements
            cycle list shift=2,
        ]
            \addplot coordinates {(0,2) (1,1) (2,1)};
    \end{groupplot}
\end{tikzpicture}
\end{document}

image showing the result of above code

Stefan Pinnow
  • 29,535
0
  • Simply, place the \caption{} (and \label{}) inside the figure
    environment not inside the subfigure to associate it with the
    figure not the corresponding subfigure. (And use
    \usepackage{subcaption})
  • Place \caption{} before the
    subfigure-s to place it at the top.
  • Use
    \captionsetup{justification=raggedright,singlelinecheck=false}
    before the \caption{} to align it left. (\usepackage{caption})

My MWE:

\documentclass{article}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}

\usepackage{caption}                %% to play with captions
\usepackage{subcaption}             %% for \begin{subfigure}
%\usepackage{tikz}              %% for tikz
\usepackage{pgfplots}               %% for \pgfplotsset

\begin{document}

\begin{figure}[tbh]             %% there is no {...
  \captionsetup{justification=raggedright,singlelinecheck=false}
  \caption{...}                 %% inside the figure not the subfigure
  \begin{subfigure}[b]{0.3\textwidth}       %% not \subfigure[]{...
    \includegraphics[width=0.15\textwidth]{solidtex.png}
  \end{subfigure}               %% not ...}
  \begin{subfigure}[b]{0.3\textwidth}       %% not \subfigure[]{...      
    \includegraphics[width=0.15\textwidth]{solidtex.png}
  \end{subfigure}               %% not ...}
  \begin{subfigure}[b]{0.3\textwidth}       %% not \subfigure[]{... 
     \includegraphics[width=0.15\textwidth]{solidtex.png}
  \end{subfigure}               %% not ...}
  \label{...}
\end{figure}

\end{document}
Tom Solid
  • 839