LaTeX Plot Snippets

The LaTeX package tikz contains a set of commands that can render vector based graphs and plots. This post contains several examples which are intended to be used as a cut-and-paste boilerplate. Each sample comes with a screenshot and a snippet that contains the relevant parts of the LaTeX source code. The entire source code can be examined by clicking onto the individual images.

Scatter Plot

Simple 2D LaTeX Plot
This type of plot visualizes one or more sets of 2D data.
\begin{tikzpicture}

  \pgfplotsset{
      scale only axis,
  }

  \begin{axis}[
    xlabel=$A$,
    ylabel=$B$,
  ]
    \addplot[only marks, mark=x]
    coordinates{ % plot 1 data set
      (0.31,0.28)
      (0.24,0.50)
      (0.52,0.1)
      % more points...
    }; \label{plot_one}

    % plot 1 legend entry
    \addlegendimage{/pgfplots/refstyle=plot_one}
    \addlegendentry{plot 1}
  \end{axis}

\end{tikzpicture}

Line Chart

Simple 2D LaTeX Line Plot
Shows one or more sets of connected points.
\begin{axis}[
  xmin=0, xmax=2000,
  ymin=0, ymax=1,
  xtick distance=500,
  xlabel=Iterations $n$,
  ylabel=$\mathrm{RMSD}(\hat{\theta})$,
]
  \addplot[] % no additional information passed to addplot
    coordinates{ % plot 1 data set
      (1,0.716)
      (2,0.6686) % ...

 

Two Y-Axes Chart

Two y-axes LaTeX Line Plot
Two different y-axis scalings for two sets of connected points.
\begin{axis}[
  axis y line*=left,
  xlabel=$x$,
  ylabel=$y_1$,
]
  \addplot[mark=*]
    coordinates{
      (.1,5)
      (.2,10)
      % more points
    }; \label{plot_1_y1}

\end{axis}

\begin{axis}[
  axis y line*=right,
  axis x line=none,
  ylabel=$y_2$,
]
  \addplot[mark=x]
    coordinates{
      (0.18,0.89)
      (0.27,0.81)
      % more points
    }; \label{plot_1_y2}
  
  \addlegendimage{/pgfplots/refstyle=plot_1_y1}\addlegendentry{$y_1$}
  \addlegendimage{/pgfplots/refstyle=plot_1_y2}\addlegendentry{$y2$}
\end{axis}

Function Graph

Tikz can render mathematical functions such as $0.2x^3-x+1$ or $3\sin x$ (here in red).
\begin{axis}[
  xlabel=$x$,
  ylabel=$y$,
  samples=100,
  ]
  \addplot[][domain=-4.5:4]{0.2*x^3-x+1};
  \addplot[draw=red][domain=-4.5:4]{3*sin(deg(x))};
\end{axis}

3D-Plot

Scatter plot with 3D data.
\begin{axis}[
  view={20}{10},
  legend pos=outer north east,
  xlabel=$A$,
  ylabel=$B$,
  zlabel=$\Gamma$,
  xmin=5.5, xmax=11,
  ymin=45.5, ymax=48,
  zmin=0, zmax=7,
  ]

  \addplot3[
    samples = 60,
    samples y=0,
    only marks,
    mark=*,
  ]
  coordinates {
    (7.23756,45.9033456,5)
    (8.712544,47.262364,4)
    % more data
  }; \label{c1}

Histogram

Colored histogram (aka. bar chart) sample.
\begin{axis}[
  area style,
]
  \addplot+[ybar interval,mark=no] plot coordinates { 
    (0,.3)
    % more data
  };
\end{axis}

Further reading: Plenty of tikz examples can be found at http://www.texample.net/tikz/examples/

Published by

Timo Denk

Software developer at SAP and Denk Development, student of Applied Computer Science at Baden-Württemberg Cooperative State University. Interested in programming, math, microcontrollers, and sports.

Leave a Reply

Your email address will not be published. Required fields are marked *