# Automatic data recognition and plot processing in pgfplots

I am a beginner in LaTeX, trying to use it for industrial documents, such a measurements report. One example is the true position graph of the MWE. The circle represents the true position tolerance. Data point outside of the circle do not meet the requirements.

I would like to improve it in the following way:

1. Specify the limits of the graph automatically (i.e. by extending the max value found either in X or Y columns of a certain factor, the same for the X axis and Y axis) fom the actual data, and not manually in the following lines:

xmin = -0.35, xmax =  0.35,
ymin = -0.35, ymax =  0.35,

2. In a similar way, the vertical and horizontal lines created by the following lines should adapt automatically to the graph size.

 \addplot+[sharp plot, mark=none, black] coordinates {(-0.50,0)  (0.5,0)};
\addplot+[sharp plot, mark=none, black] coordinates {(0,-0.5)  (0,0.5)};

3. If possible, assign the color of the dots, not on the Y value, but on the polar distance to the center.

4. Align vertically the color bar labels i.e. on the decimal separator, as opposed to the current position.

5. As a bonus, assign a neutral (i.e. blue) color to all dots that meet the tolerance, (are inside or on the circle) , and color code only the ones that do not meet the requirements...

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage[active,tightpage]{preview} %produces the plot only when activated
\PreviewEnvironment{tikzpicture} %produces the plot only when activated
\pgfplotsset{compat=1.5.1}
\usepackage{filecontents}

\begin{document}
\thispagestyle{empty}
\begin{filecontents}{Data.dat}
X       Y
0.059   0.207
-0.030  0.198
-0.035  0.042
0.064   0.130
-0.118  0.133
-0.128  0.195
-0.143  0.022
-0.148  0.182
-0.079  -0.005
-0.081  0.124
-0.091  0.126
-0.090  0.261
-0.049  -0.037
-0.045  0.063
-0.048  0.124
-0.051  0.199
-0.040  -0.063
-0.025  0.035
-0.028  0.117
-0.057  0.270
-0.020  -0.075
-0.022  0.025
-0.024  0.138
-0.33   0.288
0.012   -0.111
0.069   0.004
0.014   0.101
-0.021  -0.293
\end{filecontents}

\pgfplotstableread{Data.dat}\mytable % Creating a macro containin the data of external file

\tikzset{every mark/.append style={scale=0.7}} %Adjusting size of marks in plot

\begin{tikzpicture}
\begin{axis}[scale= 2, width=8cm,height=8cm,
%axis on top,
ticklabel style={/pgf/number format/.cd, fixed, precision=2,use comma},
xmin = -0.35, xmax =  0.35, %Plot X size
ymin = -0.35, ymax =  0.35, % Plot Y size
xlabel = {True position (mm), X axis},
ylabel = {True position (mm), Y axis},
title= Pin tip location (solder side),
colorbar, colorbar style={ticklabel style={/pgf/number format/.cd, fixed, fixed zerofill, precision=2,use comma}},
]

table {\mytable};
\addplot+[sharp plot, mark=none, black] coordinates {(-0.50,0)  (0.5,0)};
\addplot+[sharp plot, mark=none, black] coordinates {(0,-0.5)  (0,0.5)};
\end{axis}
\end{tikzpicture}

\end{document}

-
you have asked several questions. Try and make the question more general and edit, for example: "Automatic data recognition and plot processing in pgfplots". This is pretty much what you are after, by a quick scouring. –  zeroth Jul 19 '12 at 12:08
Your first question needs enlargelimits,axis equal instead of the given options, the second issue is a wrong application and you can use one of the solutions in how-can-i-add-a-zero-line-to-a-plot. The third-fifth questions are also asked on this site before. Please search for items together with the [pgfplots] string which will search only in pgfplots tagged questions. Basically you want to create a new column or do some vector lenght calculation. If you get stuck on one of these ask them seperately. –  percusse Jul 19 '12 at 12:30
@zeroth I have edited the title as suggested. I originally thought that combining all my concerns into a single set of questions would be easier for a fine expert to answer all of htem at once, as Jake did later. –  Yves Jul 19 '12 at 19:38
@Yves, yes, it is simply not the policy of the site. It is more intented to be a lookup site which requires that questions are not too localized. :) I wish you a pleasent stay here at TeX stack exchange! :) –  zeroth Jul 19 '12 at 20:44

1. I assume you want to keep the origin in the centre of the plot, so enlargelimits, axis equal won't help here. We'll need to calculate the greatest absolute value in the table and use that to set the xmin, xmax, etc. I've written a macro \findmaxAbs{<table>}{<first column name>}{<second column name>}{<fraction>} that finds the maximum absolute value, increases it by <fraction> and stores it in \maxAbs. You can then use that to set the limits.

2. As percusse said, you can use one of the approaches from How can I add a zero line to a plot?.

3. The colour is defined by the meta value, which you can set to anything you like using meta expr=< expression >. In this case, you could use meta expr={sqrt((\thisrow{X})^2+(\thisrow{Y})^2)} to get the euclidean distance. By setting point meta min=0.1, the colour bar will start from 0.1.

4. That's trickier, I think that should be a separate question.

5. You can use the scatter/@pre marker code/.append code for this. There's a nice example in the manual.

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepgfplotslibrary{colormaps}
\usepackage[active,tightpage]{preview} %produces the plot only when activated
\PreviewEnvironment{tikzpicture} %produces the plot only when activated
\pgfplotsset{compat=1.5.1}
\usepackage{filecontents}

\newcommand{\findmax}[3]{
\pgfplotstablesort[sort key={#2},sort cmp={float >}]{\sorted}{#1}%
\pgfplotstablegetelem{0}{#2}\of{\sorted}%
\pgfmathsetmacro#3{\pgfplotsretval}
}

\newcommand{\findmin}[3]{
\pgfplotstablesort[sort key={#2},sort cmp={float <}]{\sorted}{#1}%
\pgfplotstablegetelem{0}{#2}\of{\sorted}%
\pgfmathsetmacro#3{\pgfplotsretval}
}

\newcommand{\findmaxAbs}[4]{
\findmax{#1}{#2}{\maxX}
\findmax{#1}{#3}{\maxY}
\findmin{#1}{#2}{\minX}
\findmin{#1}{#3}{\minY}
\pgfmathsetmacro\maxAbs{(1+#4)*max(abs(\maxX),abs(\maxY),abs(\minX),abs(\minY))}
}

\begin{document}
\thispagestyle{empty}
\begin{filecontents}{Data.dat}
X       Y
0.059   0.207
-0.030  0.198
-0.035  0.042
0.064   0.130
-0.118  0.133
-0.128  0.195
-0.143  0.022
-0.148  0.182
-0.079  -0.005
-0.081  0.124
-0.091  0.126
-0.090  0.261
-0.049  -0.037
-0.045  0.063
-0.048  0.124
-0.051  0.199
-0.040  -0.063
-0.025  0.035
-0.028  0.117
-0.057  0.270
-0.020  -0.075
-0.022  0.025
-0.024  0.138
-0.33   0.288
0.012   -0.111
0.069   0.004
0.014   0.101
-0.021  -0.293
\end{filecontents}

\pgfplotstableread{Data.dat}\mytable % Creating a macro containin the data of external file

\findmaxAbs{\mytable}{X}{Y}{0.1}

\tikzset{mark size=2} %Adjusting size of marks in plot
\makeatletter
\begin{tikzpicture}
\begin{axis}[scale= 2, width=8cm,height=8cm,
%axis on top,
ticklabel style={/pgf/number format/.cd, fixed, precision=2,use comma},
xmin=-\maxAbs, xmax=\maxAbs,
ymin=-\maxAbs, ymax=\maxAbs,
extra y ticks       = 0,
extra y tick labels = ,
extra y tick style  = { grid = major },
extra x ticks       = 0,
extra x tick labels = ,
extra x tick style  = { grid = major },
axis equal,enlargelimits=0,
xlabel = {True position (mm), X axis},
ylabel = {True position (mm), Y axis},
title= Pin tip location (solder side),
colorbar, colorbar style={ticklabel style={/pgf/number format/.cd, fixed, fixed zerofill, precision=2,use comma}},
point meta=explicit,
colormap={errormap}{color(0cm)=(yellow); color(1cm)=(red)},
scatter/@pre marker code/.append code={%
\pgfkeys{/pgf/fpu=true,/pgf/fpu/output format=fixed}
\pgfmathtruncatemacro\iserror{\pgfplotspointmeta>0.1}
\ifnum\iserror=0
\def\markopts{fill=blue, draw=blue}%
\else
\def\markopts{}%
\fi
\expandafter\scope\expandafter[\markopts]
\pgfkeys{/pgf/fpu=false}
},%
scatter/@post marker code/.append code={%
\endscope
},
point meta min=0.1
]