5

I want to plot the following data in latex :

enter image description here

I have plotted it with libreoffice calc 5 on LinuxMint and look like below:

enter image description here

I want to do the same task with latex(tikzpicture)? I have tried to do this from the code given here : How to make a plot from table data?,

but failed. How can I do this please help

Added :

Here's a minimal example:

I called the file data.dat:

P $Q_B$ $Q_C$ $Q_D$
A1  2   6   7
A2  3   65  87
A3  1   45  65
A4  4   54  45
A5  6   56  34
A6  8   34  23
A7  9   45  45
A8  13  23  56
A9  4.6 6   87
A10 4   56  89

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_B$]{data.dat};
\addlegendentry{$Q_B$ series}
\addplot table [y=P, x=$Q_C$]{data.dat};
\addlegendentry{$Q_C$ series}
\addplot table [y=P, x=$Q_D$]{data.dat};
\addlegendentry{$Q_D$ series}
\end{axis}
\end{tikzpicture}

\end{document}
  • @JosephWright I have updated my post – Primo Feb 22 '17 at 19:54
9

Some explanation

  1. Your first row is a string type. So you have to say this.
  2. You need a floating point number for x. So I added an extra row.
  3. By default not all xticks are shown. The default width of a pgfplot is 240pt. If you want to show all use the option xtick=data.

The example:

\RequirePackage{filecontents} 
\begin{filecontents}{data.dat}
X P $Q_B$ $Q_C$ $Q_D$
1 A1  2   6   7
2 A2  3   65  87
3 A3  1   45  65
4 A4  4   54  45
5 A5  6   56  34
6 A6  8   34  23
7 A7  9   45  45
8 A8  13  23  56
9 A9  4.6 6   87
10 A10 4   56  89
\end{filecontents}

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

\pgfplotstabletypeset[columns/P/.style={string type}]{data.dat}

 \vspace{1cm}

\begin{tikzpicture}
\begin{axis}[
  xlabel=Q Series,
  ylabel=P Values,
  xticklabels from table={data.dat}{P},xtick=data]
\addplot[blue,thick,mark=square*] table [y=$Q_B$,x=X]{data.dat};
\addlegendentry{$Q_B$ series}
\addplot[red,thick,mark=square*] table [y=$Q_C$,x=X]{data.dat};
\addlegendentry{$Q_C$ series}
\addplot[black,dashed,thick,mark=square*]  table [y=$Q_D$,x=X]{data.dat};
\addlegendentry{$Q_D$ series}
\end{axis}
\end{tikzpicture}

\end{document}

enter image description here

  • 1
    Thanks for your answer. Would you tell me please why labels $A_1, A_2$ are not showing below x-axis? – Primo Feb 22 '17 at 20:18
  • You are missing xtick = {1,...,10}, --- otherwise some tick is optimized away (and arguably that could be a bug...). – Rmano Feb 22 '17 at 20:23
  • @Rmano@Primo: It's not a bug. It's a predefined setting. You can change this by options. – Marco Daniel Feb 22 '17 at 20:33
  • Yep --- but the point at A10 is marked A7 in your graph. – Rmano Feb 22 '17 at 20:35
  • @Rmano: New picture ;-) – Marco Daniel Feb 22 '17 at 20:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.