Underleaf Logo
Underleaf
PricingAboutBlog
Log InGet started

Learn PGFPlots

Getting Started

  • What is PGFPlots?
  • PGFPlots for Beginners
  • Basic Setup

Legends in PGFPlots

Create and customize legends to explain your plot data.

Basic Legend

\begin{axis} \addplot {x}; \addplot {x^2}; \addplot {x^3}; % Method 1: Using \legend \legend{Linear, Quadratic, Cubic} % Method 2: Using \addlegendentry after each plot % \addplot {x}; \addlegendentry{Linear} % \addplot {x^2}; \addlegendentry{Quadratic} \end{axis}

Legend Position

\begin{axis}[ % Predefined positions legend pos=north west, % Top left inside % Other options: % north east - Top right inside % south west - Bottom left inside % south east - Bottom right inside % outer north east - Outside, top right % Custom position legend style={ at={(0.5, -0.15)}, % (x, y) relative to axis anchor=north % Which part of legend is at that point } ]

Legend Styling

\begin{axis}[ legend style={ % Background fill=white, fill opacity=0.8, % Border draw=black, rounded corners=2pt, % Font font=\footnotesize, % Padding inner sep=4pt, % Shadow (optional) % drop shadow } ]

Multi-Column Legends

\begin{axis}[ legend columns=2, % 2 columns % Or for horizontal layout: legend columns=-1, % All items in one row legend style={ at={(0.5, -0.15)}, anchor=north, column sep=10pt % Space between columns } ] \addplot {x}; \addlegendentry{A} \addplot {2*x}; \addlegendentry{B} \addplot {3*x}; \addlegendentry{C} \addplot {4*x}; \addlegendentry{D} \end{axis}

Legend Cell Alignment

\begin{axis}[ legend cell align=left, % or: center, right % Cell spacing legend style={ /tikz/every even column/.append style={column sep=5pt} } ]

Legend Image Size

\begin{axis}[ legend image post style={ scale=0.8, % Scale legend markers mark size=3pt }, % Custom legend image legend image code/.code={ \draw[#1] (0cm,-0.1cm) rectangle (0.6cm,0.1cm); } ]

Reverse Legend Order

\begin{axis}[ legend reversed=true % Reverse the order of legend entries ]

Legend Outside Plot

\begin{axis}[ % Right side legend style={ at={(1.02, 0.5)}, anchor=west } ] % Or use predefined outer positions \begin{axis}[ legend pos=outer north east ] % Below the plot \begin{axis}[ legend style={ at={(0.5, -0.2)}, anchor=north, legend columns=3 } ]

Legend with Math

\begin{axis} \addplot {sin(deg(x))}; \addlegendentry{$\sin(x)$} \addplot {cos(deg(x))}; \addlegendentry{$\cos(x)$} \addplot {exp(-x)}; \addlegendentry{$e^{-x}$} \end{axis}

Skip Legend Entries

\begin{axis} \addplot {x}; \addlegendentry{Line 1} % Skip this plot in legend \addplot[forget plot] {x + 0.5}; \addplot {x + 1}; \addlegendentry{Line 2} \end{axis}

Custom Legend

\begin{tikzpicture} \begin{axis}[ % Hide automatic legend legend style={draw=none} ] \addplot[blue, thick] {x}; \label{plot:a} \addplot[red, thick] {x^2}; \label{plot:b} \end{axis} % Manual legend \node[anchor=north west] at (rel axis cs:0.05, 0.95) { \begin{tabular}{cl} \ref{plot:a} & Linear \\ \ref{plot:b} & Quadratic \end{tabular} }; \end{tikzpicture}

Next Steps

Continue learning PGFPlots:

  • Colors and Styles - Visual customization
  • Multiple Plots - Subplots and grouping
  • Axis Customization - Advanced axis options

Legends in PGFPlots

Create and customize legends to explain your plot data.

Basic Legend

\begin{axis} \addplot {x}; \addplot {x^2}; \addplot {x^3}; % Method 1: Using \legend \legend{Linear, Quadratic, Cubic} % Method 2: Using \addlegendentry after each plot % \addplot {x}; \addlegendentry{Linear} % \addplot {x^2}; \addlegendentry{Quadratic} \end{axis}

Legend Position

\begin{axis}[ % Predefined positions legend pos=north west, % Top left inside % Other options: % north east - Top right inside % south west - Bottom left inside % south east - Bottom right inside % outer north east - Outside, top right % Custom position legend style={ at={(0.5, -0.15)}, % (x, y) relative to axis anchor=north % Which part of legend is at that point } ]

Legend Styling

\begin{axis}[ legend style={ % Background fill=white, fill opacity=0.8, % Border draw=black, rounded corners=2pt, % Font font=\footnotesize, % Padding inner sep=4pt, % Shadow (optional) % drop shadow } ]

Multi-Column Legends

\begin{axis}[ legend columns=2, % 2 columns % Or for horizontal layout: legend columns=-1, % All items in one row legend style={ at={(0.5, -0.15)}, anchor=north, column sep=10pt % Space between columns } ] \addplot {x}; \addlegendentry{A} \addplot {2*x}; \addlegendentry{B} \addplot {3*x}; \addlegendentry{C} \addplot {4*x}; \addlegendentry{D} \end{axis}

Legend Cell Alignment

\begin{axis}[ legend cell align=left, % or: center, right % Cell spacing legend style={ /tikz/every even column/.append style={column sep=5pt} } ]

Legend Image Size

\begin{axis}[ legend image post style={ scale=0.8, % Scale legend markers mark size=3pt }, % Custom legend image legend image code/.code={ \draw[#1] (0cm,-0.1cm) rectangle (0.6cm,0.1cm); } ]

Reverse Legend Order

\begin{axis}[ legend reversed=true % Reverse the order of legend entries ]

Legend Outside Plot

\begin{axis}[ % Right side legend style={ at={(1.02, 0.5)}, anchor=west } ] % Or use predefined outer positions \begin{axis}[ legend pos=outer north east ] % Below the plot \begin{axis}[ legend style={ at={(0.5, -0.2)}, anchor=north, legend columns=3 } ]

Legend with Math

\begin{axis} \addplot {sin(deg(x))}; \addlegendentry{$\sin(x)$} \addplot {cos(deg(x))}; \addlegendentry{$\cos(x)$} \addplot {exp(-x)}; \addlegendentry{$e^{-x}$} \end{axis}

Skip Legend Entries

\begin{axis} \addplot {x}; \addlegendentry{Line 1} % Skip this plot in legend \addplot[forget plot] {x + 0.5}; \addplot {x + 1}; \addlegendentry{Line 2} \end{axis}

Custom Legend

\begin{tikzpicture} \begin{axis}[ % Hide automatic legend legend style={draw=none} ] \addplot[blue, thick] {x}; \label{plot:a} \addplot[red, thick] {x^2}; \label{plot:b} \end{axis} % Manual legend \node[anchor=north west] at (rel axis cs:0.05, 0.95) { \begin{tabular}{cl} \ref{plot:a} & Linear \\ \ref{plot:b} & Quadratic \end{tabular} }; \end{tikzpicture}

Next Steps

Continue learning PGFPlots:

Underleaf Logo
Underleaf

Empowering students and researchers with AI-powered tools for academic writing.

Go to appContact us

Company

PricingBlogTutorialsAffiliate Program

Free Tools

Image to LaTeXExcel to LaTeXArXiv to LaTeXTikZ GeneratorThesis GeneratorChrome ExtensionAll Tools

© 2026 Underleaf. All rights reserved.