3

I am trying to plot a graph using pgfplots. The plot is fine, my only concern is about the fact that I don't have, let's say, a linear x axis: I have the following elements: 1,2,4,5,7,8,9,10,12,13,14,15,20. These are ID number to identify some experiment.

Thus, when I plot them, there is space between 5 and 7 (the plot of course is expecting a 6) or between 15 and 20. I would like to have no space between x tick elements, as highlighted in the attached image.

\documentclass{standalone}

\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{filecontents}

\begin{filecontents*}{data.csv}
n,c1,c2
1,0,64
2,98,93
4,0,8
5,88,98
7,91,19
9,97,97
10,84,99
12,30,94
13,95,91
14,95,97
15,83,91
20,99,99
\end{filecontents*}

\begin{document}

\begin{tikzpicture}

\begin{axis}[
width=\textwidth,
height=\axisdefaultheight,
xlabel={x label},
ylabel={y label},
xtick={1,2,4,5,7,8,9,10,12,13,14,15,20}
]

\addplot[thick, blue, mark=o] table[x=n, y=c1, col sep=comma] {data.csv};
\addplot[thick, green, mark=triangle] table[x=n, y=c2, col sep=comma] {data.csv};
\end{axis}
\end{tikzpicture}
\end{document}

image

4
1

@Torbjørn T. : thanks a lot! It works...

I tried with both csv and dat format; with csv something "strange" happens: if I write

xticklabels from table={data.csv}{n},

while using csv format, it gives me error:

Package pgfplots Error: Sorry, could not retrieve column 'col sep comma, n' from table 'data.csv'.

and I solved using

instead of

xticklabels from table={data.dat}{n},

of course I changed my code using .dat format

I provide both the solutions.

dat format

\documentclass[border=2pt]{standalone}

\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{filecontents}

\begin{filecontents*}{data.dat}
n c1 c2
1 0 64
2 98 93
4 0 8
5 88 98
7 91 19
9 97 97
10 84 99
12 30 94
13 95 91
14 95 97
15 83 91
20 99 99
\end{filecontents*}


\begin{document}

\begin{tikzpicture}

\begin{axis}[
enlarge x limits=0.01, % adjust space between axis edge and plot edge
xticklabels from table={data.dat}{n}, % use the x column from the file for ticklabels
xtick=data, % add a tick at every data point,
width=\textwidth,
height=\axisdefaultheight,
xlabel={x label},
ylabel={y label}%,
]

\addplot[thick, blue, mark=o] table[x expr=\coordindex, y=c1] {data.dat};
\addplot[thick, green, mark=triangle] table[x expr=\coordindex, y=c2] {data.dat};
\end{axis}
\end{tikzpicture}

\end{document}

csv format

\documentclass[border=2pt]{standalone}

\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{filecontents}

\begin{filecontents*}{data.csv}
n,c1,c2
1,0,64
2,98,93
4,0,8
5,88,98
7,91,19
9,97,97
10,84,99
12,30,94
13,95,91
14,95,97
15,83,91
20,99,99
\end{filecontents*}


\begin{document}

\begin{tikzpicture}

\begin{axis}[
xticklabels from table={data.dat}{n}, % pay attention to .dat
xtick=data,
width=\textwidth,
height=\axisdefaultheight,
xlabel={x label},
ylabel={y label}
]

\addplot[thick, blue, mark=o] table[x expr=\coordindex, y=c1, col sep=comma] {data.csv};
\addplot[thick, green, mark=triangle] table[x expr=\coordindex, y=c2, col sep=comma] {data.csv};
\end{axis}
\end{tikzpicture}

\end{document}
0

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.