58

I have the following code that produces a line chart:

\begin{figure}[p]
    \centering
\begin{tikzpicture}
    \begin{axis}[
    use units,
    cycle list name=exotic, %This defines the color. It produces nicer colors
        width=15cm, height=8cm,     % size of the image
        grid = major,
        grid style={dashed, gray!30},
        %xmode=log,log basis x=10,
        %ymode=log,log basis y=10,
        xmin=0,     % start the diagram at this x-coordinate
        ymin=0,     % start the diagram at this y-coordinate
        %/pgfplots/xtick={0,5,...,60}, % make steps of length 5
        %extra x ticks={23},
        %extra y ticks={0.507297},
        axis background/.style={fill=white},
        ylabel=Time,
        xlabel=Number of Datapoints (n),
        y unit=s,y unit prefix=m,
        tick align=outside]

      % import the correct data from a CSV file
      \addplot table [col sep=comma,trim cells=true,y=Best Case (Ascending)] {datasets/time_mergesort.txt};
          \addplot table [col sep=comma,trim cells=true,y=Average Case (Random)] {datasets/time_mergesort.txt};
                    \addplot table [col sep=comma,trim cells=true,y=Worst Case (Descending)] {datasets/time_mergesort.txt};
                     \legend{Ascending,Random,Descending}
    \end{axis} 
\end{tikzpicture}
    \caption{Mergesort Run Time for Sorting Random Data}
    \label{fig:merge_sort_from_csv}
\end{figure}

And at the moment the legend appears on the chart on the upper right corner. I need to move that below the chart, or even to leave that on the chart but to move that on the upper left corner.

I have tried the solution mentioned at this question, but it did not work for me.

How can I move the axis in a different position?

2
  • 4
    For the upper left corner: legend pos=north west. See the manual (Section 4.9.5) for outer legend positions. It is much easier for others to help you if you provide minimal but complete examples. In this sample you have many lines of unrelated options that have nothing to do with the question. But you also leave out \documentclass{<...>} and package loading, requiring us to type extra things to produce a document. Please see our info on minimal working examples (MWEs). Commented Jul 5, 2014 at 21:22
  • @PaulGessler Do you want to answer, or close as unclear? Commented Feb 7, 2015 at 22:08

1 Answer 1

90

pgfplots defines several shortcut legend pos settings, as defined in Section 4.9.5 of its manual; here you could choose legend pos=north west or legend pos=outer north east, which is currently the only pre-defined legend position outside the axis box.

If you want to place it in any other location, the legend pos keys are just shorthand for

legend style={at={(<x>,<y>)},anchor=<name>}

where <x> and <y> are in terms of the rel axis cs (ranges from 0 to 1 over the width and height of the axis box.

Here are a few possibilities:

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}

\begin{document}
\begin{tikzpicture}
\begin{axis}[legend pos=north west]
  \addplot {x};
  \addlegendentry{$x$}
  \addplot {x^2};
  \addlegendentry{$x^2$}
\end{axis}
\end{tikzpicture}

\begin{tikzpicture}
\begin{axis}[legend pos=outer north east]
  \addplot {x};
  \addlegendentry{$x$}
  \addplot {x^2};
  \addlegendentry{$x^2$}
\end{axis}
\end{tikzpicture}

\begin{tikzpicture}
\begin{axis}[legend style={at={(0.5,-0.1)},anchor=north}]
  \addplot {x};
  \addlegendentry{$x$}
  \addplot {x^2};
  \addlegendentry{$x^2$}
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

enter image description here

enter image description here


More general solution prompted by a comment

The original answer focused on cases where there was no xlabel. If there is one, and you want the legend to appear below the xlabel, you could manually adjust the legend's x- and y- coordinates in the at key value. But if the size of your label or the proportions of the axis change, the required coordinates may also change.

So IMHO a better way is to use a named legend. This allows you to print the legend separately after the current axis is closed and its size/bounding box is known. See section 4.9.7 of the current pgfplots manual for full details. It could be argued that this is better than my original at={(<x>,<y>)} solution even in cases where there is no xlabel, because this avoids needing to come up with magic numbers for the coordinates.

Adapting the examples above, this new method would look something like this:

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}

\begin{document}
\begin{tikzpicture}
\begin{axis}[legend to name=myfancyname,xlabel={Test}]
  \addplot {x};
  \addlegendentry{$x$}
  \addplot {x^2};
  \addlegendentry{$x^2$}
\end{axis}
\node[anchor=north] at (current axis.below south) {\ref{myfancyname}};
\end{tikzpicture}
\end{document}

With the result:

enter image description here

3
  • What if I have an xlabel below the lowest plot?
    – mcmuffin6o
    Commented Feb 16 at 0:11
  • 1
    @mcmuffin6o good question, that lead me to come up with a more generalized answer than my original. Updates to the answer coming shortly. Commented Feb 16 at 23:14
  • 1
    Holy moly! That really is quite nice. I'm a noob to pgfplots, so concise examples such as yours are incredibly helpful.
    – mcmuffin6o
    Commented Feb 17 at 22:10

You must log in to answer this question.

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