# How to stop pgfplots scatter to sort the data table?

I want to draw a scatter plot, using pgfplots and I want the data to appear in the plot area the same as it is listed in the table. I don't want the data to be sorted and then be plotted.

As it is visible, the most left data to be plotted should be (x=7,y=10) and descending come to (6,2) but the most left plotted data is now (1,5) which means that it is sorted automatically.

%pdflatex

\documentclass[standalone]{book}
\usepackage{tikz,pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[%
scatter/classes={%
a={mark=o,draw=black}}]
table[meta=label] {
x y label
7 10 a
10 8 a
8 7 a
4 6 a
9 5 a
1 5 a
5 4 a
3 4 a
2 3 a
6 2 a
};
\end{axis}
\end{tikzpicture}

\end{document}

• Huh? Sorry I don't understand. How do you determine the the data was sorted just from the graph? And why should it matter if the data points are sorted or not. You want them all plotted, don't you? – Peter Grill Oct 16 '14 at 22:27
• @PeterGrill I edited my question to show what I am looking for. I am not telling what is first put on the plot area. I want the data be plotted regarding the descending values of y-axis and labelled by their x-axis written in data table. – Enthusiastic Engineer Oct 16 '14 at 22:37

The default behavior of PGFplots is to use x coordinates as... well, coordinates. So naturally, the point with the lowest x value will appear on the left. What you want to achieve is to use the x column as symbolic tick labels instead of coordinates, and additionally use the line number as the proper x coordinate of the plot. The latter can be achieved with x expr=\coordindex. This removes what you call "sorting" and shows the y coordinates in the order in which they appear in the table, positions at x=0, x=1, etc...

Using the x column as symbolic tick labels is a bit trickier. This answer by Jake demonstrates how to do that by defining a new key xticklabels from table, which has to be used on the axis environment. Additionally, the data table has to be provided to the key and the addplot command, because the tick labels belong to the axis, which can contain multiple plots, but only one set of ticks. Also, you must specify xtick=data to get a tick for each data point in your table.

The full example looks like this:

\documentclass[standalone]{book}
\usepackage{tikz,pgfplots,pgfplotstable}

x y label
7 10 a
10 8 a
8 7 a
4 6 a
9 5 a
1 5 a
5 4 a
3 4 a
2 3 a
6 2 a
}{\MyData}

\makeatletter
\pgfplotsset{
/pgfplots/xticklabels from table/.code 2 args={%
\pgfplotstablegetcolumn{#2}\of{#1}\to\pgfplots@xticklabels
\let\pgfplots@xticklabel=\pgfplots@user@ticklabel@list@x
}
}
\makeatother

\begin{document}
\begin{tikzpicture}
\begin{axis}[%
scatter/classes={%
a={mark=o,draw=black}},
xtick=data,
xticklabels from table={\MyData}{x}
]

• You have to declare beforehand each table which you want to use with xticklabels from table. Not necessarily in the preamble though, directly before the tikzpicture would be okay too. Otherwise, you can leave them in the \addplot command or in an external file, or wherever you want. – Fritz Oct 16 '14 at 23:12
• Just add xlabel=Stuff or ylabel=Stuff to the axis options. – Fritz Oct 16 '14 at 23:15