Tx · LaTeX & TikZ

How to Combine PGFPlots with One Legend and Rotate Them

Published By the 9to5Science editorial team

At a glance

  1. 1. Group the axes

    Use PGFPlots’ groupplots library to arrange multiple axis environments as one multi-panel graphic.

  2. 2. Define one legend

    Put legend entries in one panel and save that legend with legend to name.

  3. 3. Place the legend

    Render the saved legend once below the group with pgfplotslegendfromname.

  4. 4. Rotate the graphic

    Wrap the complete tikzpicture in graphicx’s rotatebox so the panels and legend rotate together.

To make a multi-panel PGFPlots figure with one legend, group the axes and save the legend from one of them. Then place the saved legend once below the group. If the whole layout should be rotated, put the complete tikzpicture inside \rotatebox from the graphicx package.

The example below creates two side-by-side panels, one shared legend, and rotates the panels and legend together by 90 degrees.

Complete example

\documentclass{article}
\usepackage[margin=2cm]{geometry}
\usepackage{graphicx}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\pgfplotsset{compat=1.18}

\begin{document}

\begin{figure}[ht]
  \centering
  \rotatebox{90}{%
    \begin{tikzpicture}
      \begin{groupplot}[
        group style={group size=2 by 1, horizontal sep=1.2cm},
        width=5cm,
        height=4cm,
        xmin=0, xmax=2,
        ymin=0, ymax=4,
      ]
        \nextgroupplot[
          title={First panel},
          legend to name=sharedlegend,
          legend columns=2,
        ]
          \addplot[blue, thick] coordinates {(0,0) (1,1) (2,4)};
          \addlegendentry{Series A}
          \addplot[red, thick] coordinates {(0,0) (1,2) (2,4)};
          \addlegendentry{Series B}

        \nextgroupplot[title={Second panel}]
          \addplot[blue, thick] coordinates {(0,0) (1,2) (2,3)};
          \addplot[red, thick] coordinates {(0,1) (1,2) (2,3)};
      \end{groupplot}

      \node[anchor=north, yshift=-8mm] at (current bounding box.south)
        {\pgfplotslegendfromname{sharedlegend}};
    \end{tikzpicture}%
  }
  \caption{Two panels with one shared legend, rotated by 90 degrees.}
\end{figure}

\end{document}

This uses the groupplots library, provided by PGFPlots, to create two axes with a consistent size and horizontal spacing. The legend to name=sharedlegend option saves a legend from the first axis instead of drawing it in that panel. The \pgfplotslegendfromname{sharedlegend} command then places that legend below the group. The node is centered at the graphic’s bottom edge, with an 8 mm gap.

Only the first axis has \addlegendentry commands. The second axis uses the same styles but does not add duplicate labels. Compile the document at least twice: the named legend is resolved through auxiliary information, so it may not appear on the first run.

Why rotate the whole picture?

\rotatebox{90}{...} rotates its contents as a single object. Because the entire tikzpicture is inside the command, both axes and the shared legend rotate together. The angle is in degrees; use -90 to rotate in the opposite direction.

If you want a landscape page rather than a rotated plot, use the pdflscape package and its landscape environment around the figure. If you only want to rotate tick labels or a legend label, use the relevant PGFPlots or TikZ rotation option instead of rotating the entire graphic.

Adding entries for series in later panels

The example builds the legend from the first panel, which is convenient when the same series appear in every panel. If a series appears only in a later panel, it will not automatically be included in the saved legend. Add a corresponding legend image and entry to the axis that creates the named legend, or construct a separate manual legend. For example, \addlegendimage{green, thick} followed by \addlegendentry{Series C} adds a green line sample to that legend without plotting extra data.

Common mistakes

  • Repeating legend entries in every axis: This creates repeated labels or multiple legends. Put entries in just one axis when you want one shared legend.
  • Placing the legend outside the rotated picture: Anything outside \rotatebox remains unrotated. Put the legend node inside the same tikzpicture if it should turn with the plots.
  • Expecting rotation to fix page fit: Rotation swaps the graphic’s apparent width and height. If it does not fit, reduce the panel dimensions or use a landscape page.
  • Forgetting the second compilation: If the named legend is missing or stale, compile again to resolve the reference.

References

  1. PGFPlots Manual
  2. The LaTeX Graphics Companion

Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.