3

I have the following data in data.dat file .

TA   | OP

6    | 20
17   | 24
18   | 17
19   | 17
22   | 19
33   | 27
19   | 28
13   | 18
24   | 16
19   | 19
9    | 18
21   | 22
16   | 10
19   | 12
22   | 19
26   | 19
27   | 19
11   | 11
25   | 16
15   | 17
24   | 22
24   | 17
21   | 20

Here TA means Traditional approach and OP means Our Approach . I have to show a relation between these two columns . For this purpose I have the following code:

\begin{tikzpicture}[x=0.4cm,y=0.4cm]

  \def\xmin{0}
  \def\xmax{40}
  \def\ymin{0}
  \def\ymax{40}

  % grid
  \draw[style=help lines, ystep=1, xstep=1] (\xmin,\ymin) grid
  (\xmax,\ymax);

  % axes
  \draw[->] (\xmin,\ymin) -- (\xmax,\ymin) node[right] {$x$};
  \draw[->] (\xmin,\ymin) -- (\xmin,\ymax) node[above] {$y$};

  % xticks and yticks
  \foreach \x in {1,2,3,...,40}
    \node at (\x, \ymin) [below] {\x};
  \foreach \y in {1,2,3,...,40}
    \node at (\xmin,\y) [left] {\y};

  % plot the data from the file data.dat
  % smooth the curve and mark the data point with a dot
  \draw[color=blue] plot[smooth,mark=*,mark size=1pt] file {data.dat}
   node [right] {data};

\end{tikzpicture}

But surprisingly I have got the following picture:

enter image description here

Why ? What Have I to do to get the right relationship ?

The graph that shows the relationship between two column is as follows:

enter image description here

1
  • I'd suggest you edit your question quite heavily: what you are after is plotting data against the implicit row number in a table. That doesn't come across at all in the description: it's only the final figure that gives it away.
    – Joseph Wright
    Jul 19 '14 at 6:36
10

Your data is not sorted so TikZ goes back and forth to draw the points. I would really recommend pgfplots for this. I've sorted your table and plotted again

\documentclass{standalone}
\usepackage{pgfplotstable}%<- Loads pgfplots anyway
\pgfplotsset{compat=1.10}
\pgfplotstableread[header=false]{data.dat}\mytable
\pgfplotstablesort{\mysortedtable}{\mytable}

\begin{document}
\begin{tikzpicture}[x=0.4cm,y=0.4cm]
\begin{axis}[xmin=0,xmax=40,ymin=0,ymax=40,grid=both]
\addplot+[smooth,mark size=1pt] table {\mysortedtable}  node [right] {data};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

Notice at 19,22 and 24 you have more than one sample per x.

What you say as a relation is really confusing but if you have the data file without the | rule character, you can do the following

\documentclass{standalone}
\usepackage{pgfplotstable}%<- Loads pgfplots anyway
\pgfplotsset{compat=1.10}
\pgfplotstableread[]{data.dat}\mytable


\begin{document}
\begin{tikzpicture}[x=0.4cm,y=0.4cm]
\begin{axis}[xmin=0,xmax=25,ymin=0,ymax=40,grid=both]
\addplot+[smooth,mark size=1pt] table[x expr=\coordindex,y=TA] {\mytable};
\addplot+[smooth,mark size=1pt] table[x expr=\coordindex,y=OP] {\mytable};
\legend{Traditional Approach, Our Approach}
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

6
  • Why are here only one curve in this graph ? I have to show relationship between the two column . So there should be two column Jul 18 '14 at 18:47
  • @user54714 Two columns with respect to what?
    – percusse
    Jul 18 '14 at 22:21
  • between each other . Jul 19 '14 at 3:37
  • @user54714 And this is between each other. x is first y is second column, what exactly do you mean?
    – percusse
    Jul 19 '14 at 6:25
  • Typical: I was half-way though writing an answer using \coordindex for the implicit row number :-)
    – Joseph Wright
    Jul 19 '14 at 6:34
4

Would this be what you try to achieve? Here the plot options ybar with filled blue colors is used. There are many options you could try, say, sharp plot, only marks, const plot, const plot mark left, ycomb etc. But in this case, ybar and only marks are probably better choices, IMHO, for clarity.

enter image description here

Code

\documentclass[border=10pt]{standalone}%{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}[x=0.4cm,y=0.4cm]

  \def\xmin{0}
  \def\xmax{40}
  \def\ymin{0}
  \def\ymax{40}

  % grid
  \draw[style=help lines, ystep=1, xstep=1]   (\xmin,\ymin) grid
  (\xmax,\ymax);

  % axes
  \draw[->] (\xmin,\ymin) -- (\xmax,\ymin) node[right] {$x$};
  \draw[->] (\xmin,\ymin) -- (\xmin,\ymax) node[above] {$y$};

  % xticks and yticks
  \foreach \x in {1,2,3,...,40}
    \node at (\x, \ymin) [below] {\x};
  \foreach \y in {1,2,3,...,40}
    \node at (\xmin,\y) [left] {\y};

  % plot the data from the file data.dat
  % smooth the curve and mark the data point with a dot
  \draw[color=blue,fill] plot[smooth,mark=*,mark size=1pt, ybar] file {data.dat};  
  \node [color=blue] at (22,24)  {data};

\end{tikzpicture}
\end{document}

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.