[Tex/LaTex] How to generate stem plots with TikZ like stem() in Matlab

pgfplotsplottikz-pgf

Given a set of (x,y) coordinates stored in a file, how could I generate a stem plot from those coordinates? I would like to achieve something similar as the Matlab's stem function.

I found a nice tutorial on how to generate 2D plots, but the points are interpolated.

Best Answer

Package pgfplots is a good replacement for gnuplots and Matlab figures. Here you have an example. It's easy to understand its manual.

\documentclass[border=3mm]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{filecontents}
\begin{filecontents*}{datafile.dat}
3.045784        3.415896
3.405784        4.025693
3.785784        4.522530
4.125784        5.538449
4.485784        6.704992
4.805784        6.978939
5.145784        7.113496
5.425784        8.916397
6.065784        9.487712
6.365784        10.876397
6.685784        10.693497
7.025784        11.364131
7.345784        11.442530
7.665784        12.582530
8.005784        13.125693
8.225784        13.738450
8.585784        14.247891
8.865784        14.982530
\end{filecontents*}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot+[ycomb] plot table[x expr=\coordindex+1, y index=0]{datafile.dat};
\addplot+[ycomb] plot table[x expr=\coordindex+1, y index=1]{datafile.dat};
\end{axis}
\end{tikzpicture}
\end{document}

The result is

enter image description here

If you are already using Matlab, matlab2tikz will be a good tool.

Related Question