[Tex/LaTex] How to make a plot from table data

pgfplotstabletikz-pgf

Having the following table:

enter image description here

How can we make a plot diagram showing the relation between P,Q? I would like the P-value on y-axis and the Q-values on the x-axis.

Best Answer

Here's a minimal example:

Assuming you have the data in a file (I called the file data.dat):

P $Q_A$ $Q_B$ $Q_D$
10 5 7 12
8 8 10 18
6 12 16 28

Use the code:

\documentclass{article}
\usepackage{pgfplotstable}
\usepackage{pgfplots}
\pagestyle{empty}
\begin{document}

\pgfplotstabletypeset{data.dat}

\vspace{1cm}

\begin{tikzpicture}
\begin{axis}[
  xlabel=Q Series,
  ylabel=P Values]
\addplot table [y=P, x=$Q_A$]{data.dat};
\addlegendentry{$Q_A$ series}
\addplot table [y=P, x=$Q_B$]{data.dat};
\addlegendentry{$Q_B$ series}
\addplot table [y=P, x=$Q_D$]{data.dat};
\addlegendentry{$Q_D$ series}
\end{axis}
\end{tikzpicture}

\end{document}

enter image description here

Check out the documentation for pgfplots. It's very well suited to creating graphs. I've only demonstrated the most basic possibilities. But it has a lot of power.

Note if you're going to use the package pgfplotstable then, technically, there's no need to load pgfplots. But, if you don't use pgfplotstable then be sure to load pgfplots to be able to graph your data.