1 minute read

In this tutorial, we will see how to create multiple line-plot using Latex Tikz and PGFPlot.

Let’s first create a document and import the necessary packages


Now, let’s create a dataset, or you can upload a CSV/DAT file.


Now, let’s create a \begin{tikzpicture}...\end{tikzpicture} environment and put that within the \begin{figure}...\end{figure} environment. All of our code will be within the tikzpicture environment.

\pgfplotstableread[col sep=comma,]{testdata.csv}\datatable
    xticklabels from table={\datatable}{x-axis},
    x tick label style={font=\normalsize, rotate=35, anchor=east},
    legend style={at={(0.98,0.3)},anchor=south east},
    \addplot [mark=o, blue!80 ] table [x expr=\coordindex, y={a}]{\datatable};
    \addplot [mark=o, red!80] table [x expr=\coordindex, y={b}]{\datatable};
    \addplot [mark=o, black!50 ] table [x expr=\coordindex, y={c}]{\datatable};
    \addplot [mark=o, violet!80] table [x expr=\coordindex, y={d}]{\datatable};
    \addplot [mark=o, cyan!80] table [x expr=\coordindex, y={e}]{\datatable};
    \addplot [mark=o, orange!30] table [x expr=\coordindex, y={f}]{\datatable};
    \addplot [mark=o, brown!90] table [x expr=\coordindex, y={g}]{\datatable};
    \addplot [mark=o, green!80!red!80] table [x expr=\coordindex, y={h}]{\datatable};
\caption{Multiple Line-plots using Tikz}

Here, \pgfplotstableread is used to read the file contents and declare as a new table name.

Within the \axis environment, first we define the width, height, xtick information, and legend place. Then we use \addplot for each column to convert to individual line plots. \addlegendentry is used to add an entry for that line in the legendbox.

The output can be viewed in the Overleaf Document

That’s it for today! Cheers!!!

You can find a comprehensive list of Latex resources in the following post:

Latex Resources in a Nutshell

If you are a new Latex user, check out this post: 20 Most Common Mistakes Made by New Latex Users

You can find all Latex oriented posts of mine in: https://shantoroy.com/categories/#latex

Leave a comment